mvn install vs jar:jar

2020-06-23 07:11发布

问题:

What is the difference between the "mvn install" command, versus the use of the jar:jar plugin ?

Its clear that "install" seems to build a jar, and so, I am wondering what the need for the jar:jar plugin would be.

回答1:

mvn install command will "execute" the maven lifecycle up to the install phase. It means that all previous phases will be executed (including the package phase).

On a simple maven jar project, the package phase is bind to the maven-jar-plugin. And so executing mvn install will execute at some point jar:jar.



回答2:

There are two types of things you can specify on the maven command line:

  • lifecycle phases (these do not include a : character)

  • plugin goals (these include at least one : character, depending on how fully you specify the plugin, it could be short-name:goal or groupId:artifactId:goal or groupId:artifactId:version:goal)

There are three lifecycles: default, clean and site. Each lifecycle consists of a series of phases. When you specify a phase in a lifecycle then Maven will execute in order all the phases in that lifecycle up to and including the specified phase.

When you specify a plugin goal then that plugin goal is invoked and only that plugin goal.

Maven has a concept of a packaging which defines a default set of plugin bindings to lifecycle phases. For example the jar packaging (which is default unless your pom.xml includes a <packaging>...</packaging> element) by default binds jar:jar to the package phase and binds install:install to the install phase.

So when you type

$ mvn package

Maven will run all the way through the lifecycle phases executing plugins that are bound (either from the lifecycle or by specifying plugin executions in the pom) as it goes along.

When you type

$ mvn jar:jar

Maven will just run the jar plugin's jar goal.

The lifecycle is 99 times out of 100 the one you want to use.

Here are the times you would typically want to invoke plugin goals directly

  • jetty:run to start a webapp server

  • surefire:test to quickly re-run the tests (usually with -Dtest=... to specify the specific one

  • release:prepare release:perform to release your code

  • versions:... to do some updating or querying of version related stuff, e.g. versions:display-plugin-updates

  • ship:ship or cargo:deployer-deploy to push (ship) your built artifacts to a hosting environment



回答3:

install puts the artifact in your local (on your machine) maven repository, jar:jar does not. If you call jar:jar on a library then try to reference that library in another project, it will not be in your local repository.

Also note that mvn package is a cleaner way to do packaging, rather than using jar:jar.