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.
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
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.
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.
Related
From what I read, maven cycles through the lifecycle phases and goals invoked through the command line and it simply cycles through the mentioned phases and goals. The defaults in pom.xml make sure that maven runs sensible defaults by always running a series of plugin goals according to packaging using default bindings.
Also, if I only and only want to download dependencies and do nothing else, I can call mvn dependency:generate-sources.
So my question is:
If we run mvn install, it also downloads the dependencies mentioned in the pom.xml? Does this happen because calling install calls all the phases up till install including generates-sources which is bound to dependency plugin by default?
If not, who is responsible for fetching all the dependencies? Maven core or some other plugin?
If yes, the list of plugins invoked by default does not seem to be exhaustive. What other plugin bindings exist in pom.xml?
Yes
N/A
All maven projects have, at their base the "Super POM" which lists all of the defaults for maven. You can look there for everything. This "Super POM" is placed as the parent for any POM that doesn't list a parent explicitly.
Can I execute a Maven phase (say: deploy) without implicitly calling the previous ones?
The reason: I would like to construct something like install site-deploy (only-deploy) to make sure that the deployment of the artifact only happens if all other phases/goals were successful. I cannot replace (only-deploy) with deploy:deploy because some projects which use this configuration have additional goals in the deploy phase.
No, it is called lifecycle for a reason. When we start with the next major release of Maven, we'll work on advanced lifecycle handling, where https://issues.apache.org/jira/browse/MNG-5666 is part of the solution for your issue.
Both the install and deploy plugin have an experimental xxxAtEnd, maven-site-plugin deploy goal should require such option as well.
It's (now) possible:
To run a specific goal without executing its entire phase (and the
preceding phases), we can use the command:
mvn <PLUGIN>:<GOAL>
For example, to run the integration-test goal from the Failsafe
plugin, we need to run:
mvn failsafe:integration-test
another example: mvn compiler:compile
I am using maven for the build purpose and normally we use the maven command mvn clean -Dmaven.test.skip=true package only to build the web application. I know we can use the mvn install command also to build a web application. But can anyone provide me with the exact difference between these two commands?
I found some notes on the clean and install commands. But i just want to know what's the advantage of using mvn clean command instead of using install command.
The main different between mvn clean -Dmaven.test.skip=true package and mvn install is that the first command line cleans the target directory and packages without running the tests. The second one compiles, tests, packages and installs the JAR or WAR file into the local repository at ~/.m2/repository.
Maven has this concept of Maven Phases. Please go through the Maven Phases of this doc. So when you run a phase (say maven phase x) all the phases up to that phase is executed (that is phase 1 to phase x).
You need mvn clean to clean up artifacts created by prior builds. mvn package will package your code into your specified format in your POM. mvn install will also install the package made by Maven into the local repository.
Also note that clean and site are not part of phases of the default life-cycle. You have to fire it before your package or install command. Needless to say ordering does matter here.
As explained here.
clean is its own action in Maven. mvn clean install tell Maven to do the clean action in each module before running the install action for each module.
What this does is clear any compiled files you have, making sure that you're really compiling each module from scratch.
I'm trying to get into maven and started writing just a small library. As part of the library code I want to have a small demo java application one can use to see what the library is doing and how to use it. One thing that really annoys me right now is that in order to execute that demo I have to execute two commands: mvn package and mvn exec:exec or mvn package exec:exec.
Is there any way to configure maven to automatically run "package" before "exec:exec"? So that I can run mvn exec:exec on a fresh checkout and compilation will implicitly happen before execution?
mvn package exec:exec
You can specify multiple goals for Maven to execute as part of the same command.
Additionally, when you configure the exec plugin, you can bind the execution to the package phase: Lifecycle Intro
I have a Maven Java project with many modules and one meta-modul. And I want to run test. I use: mvn test command in console. But my test fails when compilation, because classes in other modules are not found. But in IDE Eclipse no errors. How can I fix that?
if you have well defined dependencies, use mvn install instead of mvn test. Running tests is included in install phase too and you'll get the modules you need for compilation into local maven repository.
Try to run mvn install. This compile, packages and runs tests. Then when everything is compiled you can probably run mvn test only. But you should not (IMHO) because only when you are running the full process you are sure that the newest versions of your classes are being tested. Do not worry about efficiency: maven does not compile classes if it already have them compiled. Only mvn clean install will rebuild everything from scratch.
Your models need to be free of dependency cycles!
Try to run mvn test from the folder where the parent pom is located.
(details)
If you do not have a parent pom with sub modules. Then you must first run mvn install for all the other of your modules where the module you want to test depends from.
(Eclipse does not need this, because it can resolve dependencies to other open projects directly)
But if all the modules belong to an single release cycle (all the modules will be released togeter with the same version) then it is may a better approach to use parent and child modules/pom -- because then you can run mvn test or mvn install for the parent pom, and maven will do it for all the childs in the right order. -- After you have installed all other modules, you can run mvn test on a single module until you update an other modul. -- Then you will need to install this updated modul too, or better run install for the parent.