I'm new to the maven tool, I have made a project with Spring and Hibernate and they are configured in pom.xml as plugins, but JUnit is tagged under dependency. My question is what is the logic behind one as a plugin and one as dependency ?
相关问题
- Include pom.xml in Jar with gradle
- What order does maven find its dependencies?
- proguard causing EnumMap NPE on dynamically declar
- Maven: How to read the Parent POM version
- enableHiveSupport throws error in java spark code
相关文章
- IDEA2020 安装maven 插件后,springboot程序 SpringBootApplic
- pom文件中的插件定义
- pom.xml中的project为何报红
- Hibernate Tutorial - Where to put Mapping File?
- Call non-static methods on custom Unity Android Pl
- Cannot use org.jvnet.jax-ws-commons.jaxws-maven-pl
- How to obtain the enable admission controller list
- New Maven install: mvn -version java.lang.ClassNot
Both plugins and dependencies are Jar files.
But the difference between them is, most of the work in maven is done using plugins; whereas dependency is just a Jar file which will be added to the classpath while executing the tasks.
For example, you use a compiler-plugin to compile the java files. You can't use compiler-plugin as a dependency since that will only add the plugin to the classpath, and will not trigger any compilation. The Jar files to be added to the classpath while compiling the file, will be specified as a dependency.
Same goes with your scenario. You have to use spring-plugin to execute some spring executables [ I'm not sure what spring-plugins are used for. I'm just taking a guess here ]. But you need dependencies to execute those executables. And Junit is tagged under dependency since it is used by surefire-plugin for executing unit-tests.
So, we can say, plugin is a Jar file which executes the task, and dependency is a Jar which provides the class files to execute the task.
Hope that answers your question!
Plug-ins are used for adding functionalities to
Maven
itself (like addingeclipse
support orSpringBoot
support toMaven
etc.). Dependencies are needed by your source code to pass any Maven phase (compile
ortest
for example). In case ofJUnit
since the test code is basically part of your code base and you callJUnit
specific commands inside test suites and those commands are not provided byJava SDK
thereforeJUnit
must be present at the timeMaven
is in the test phase and this is handled by mentioningJUnit
as a dependency in yourpom.xml
file.Maven at its heart is a plugin execution framework -- as per formal and standard compact definition. To make it more clear, the commands you use like
maven-install/clean/compile/build etc
for creating/executing jars, which we sometimes manually run too. So, the things which you want to run (or configure or execute) you basically put them in dependency tag of mavens pom and the answer so as to who will run these dependencies (required for environment setup) be the plugins.Maven itself can be described as food processor which has many different units that can be used to accomplish different tasks. Those units are called plugins. For example, to compile your project maven uses
maven-compiler-plugin
, to run tests -maven-surefire-plugin
and so on.Dependency in terms of maven is a packaged piece of classes that your project depends on. It can be jar, war etc. For example, if you want to be able to write JUnit test, you'll have to use JUnit annotations and classes thus you have to declare that your project depends on JUnit.
If you're coming from a front-end background like me, and are familiar with Grunt and npm, think of it like this:
First you would run, say,
npm install grunt-contrib-copy --save-dev
. This is like maven's<dependency></dependency>
. It downloads the files needed to execute a build task.Then you would configure the task in Gruntfile.js
This is like maven's
<plugin>/<plugin>
. You are telling the build tool what to do with the code downloaded by npm/<dependency></dependency>
.Of course this is not an exact analogy, but close enough to help wrap your head around it.
Plugins and dependencies are very different things and these are complementary.
What plugins are ?
Plugins perform tasks for a Maven build. These are not packaged in the application.
These are the heart of Maven .
Any task executed by Maven is performed by plugins.
There are two categories of plugins : the
build
and thereporting
plugins :<build/>
element from the POM.<reporting/
> element from the POM.According to the maven goal specified in the command line (for example
mvn clean
,mvn clean package
ormvn site
) , a specific lifecyle will be used and a specific set of plugins goals will be executed.There are three built-in build lifecycles:
default
,clean
andsite
. Thedefault
lifecycle handles your project deployment, theclean
lifecycle handles project cleaning, while thesite
lifecycle handles the creation of your project's site documentation.A plugin goal may be bound to a specific phase of a specific lifecyle.
For example the
maven-compiler-plugin
binds by default thecompile
goal to the lifecycle phase:compile
.Most of maven plugins (both core plugins and third party plugins) favor convention over configuration. So these generally bound a plugin goal to a specific phase to make their usage simpler.
That is neater and less error prone :
than :
What dependencies are ?
Dependencies are Maven artifacts required during the Maven build.
These may be packaged in the application but not necessarily (see the
scope
below).The most of dependencies are jar but these may also be other kinds of archives : war, ear, test-jar, ejb-client ... or still POM or BOM.
In a pom.xml, dependencies may be specified at multiple places : the
<build><dependencies>
part , thedependencies management
part or still in aplugin
declaration ! Indeed some plugins may need to have some dependencies in the classpath during their execution. That is not common but that may happen.Here is an example from the documentation that shows that
plugin
anddependency
may work together :In Maven, dependencies are referenced in a specific format :
groupId:artifactId:packaging:classifier:version
.The classifier (that is optional) and the packaging (
JAR
by default) are not commonly specified. So the common format in thedependency
declaration is rather :groupId:artifactId:version
.Here is an example of dependency declared in the
<build><dependencies>
part :Contrary to a plugin, a dependency has a scope.
The default scope is
compile
. That is the most commonly needed scope (convention over configuration again).The
compile
scope means that the dependency is available in all classpaths of a project.The scope defines in which classpaths the dependency should be added. For example do we need it at compile and runtime, or only for tests compilation and execution ?
For example we previously defined Hibernate as a
compile
dependency as we need it everywhere : source compilation, test compilation, runtime and so for....But we don't want that testing libraries may be packaged in the application or referenced in the source code. So we specify the
test
scope for them :