Mvn install or Mvn package
Solution 1
from http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
package
: take the compiled code and package it in its distributable format, such as a JAR.
install
: install the package into the local repository, for use as a dependency in other projects locally
So the answer to your question is, it depends on whether you want it in installed into your local repo. Install will also run package because it's higher up in the goal phase stack.
Solution 2
mvn install
is the option that is most often used.
mvn package
is seldom used, only if you're debugging some issue with the maven build process.
See: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
Note that mvn package
will only create a jar file.
mvn install
will do that and install the jar (and class etc.) files in the proper places if other code depends on those jars.
I usually do a mvn clean install
; this deletes the target
directory and recreates all jars in that location.
The clean helps with unneeded or removed stuff that can sometimes get in the way.
Rather then debug (some of the time) just start fresh all of the time.
Solution 3
From the Lifecycle reference, install will run the project's integration tests, package won't.
If you really need to not install the generated artifacts, use at least verify.
Solution 4
Also you should note that if your project is consist of several modules which are dependent on each other, you should use "install" instead of "package", otherwise your build will fail, cause when you use install command, module A will be packaged and deployed to local repository and then if module B needs module A as a dependency, it can access it from local repository.
Solution 5
If you're not using a remote repository (like artifactory), use plain old:
mvn clean install
Pretty old topic but AFAIK, if you run your own repository (eg: with artifactory) to share jar among your team(s), you might want to use
mvn clean deploy
instead.
This way, your continuous integration server can be sure that all dependencies are correctly pushed into your remote repository. If you missed one, mvn will not be able to find it into your CI local m2 repository.
user2192023
Updated on March 27, 2020Comments
-
user2192023 over 4 years
I am new to Maven, I have a Java based web project with maven configured in my MyEclipse.
Now if I modified any java files then do I need to doRun as -> Mvn install
orMvn package
? -
Joshua Wilson over 10 yearsI disagree that package is seldom used on 2 points. 1) It gets run every time you run install. 2) If you are making a .war then just running package is fine as you don't need a war in your local repo.
-
Vsevolod Golovanov almost 9 yearsThe answer doesn't explain, why would you prefer to install to the local repository. In my understanding, if the projects are set up right, then the reactor will provide the dependencies between modules. If the projects are not set up right, then installing could just hide this fact and use the wrong artifacts. If you have dependent projects, that must be built separately for some reason, only then would you want to install.
-
Mayjak over 8 yearsI think you are talking about mvn clean deploy, not mvn clean package
-
MasterJoe almost 6 yearsI have a project where I am converting the main folder code & test folder code into Jar. Then, I want to deploy this into nexus so that it can be used by other projects. My deploy command is "mvn clean deploy -DskipTests". Can I make this command package, rather than install to local repo ?
-
walen over 5 years@MasterJoe2 "Then, I want to deploy this into nexus" That's what
deploy
does. If you replace it withpackage
, it won't be deployed to Nexus. -
Brent Bradburn over 3 yearsThe linked docs say "If you are uncertain what you want, the preferred phase to call is
mvn verify
". So this is a pretty definitive answer to the original question.