Mvn install or Mvn package

JavaMavenMyeclipse

Java Problem Overview


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 do Run as -> Mvn install or Mvn package?

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 - Java

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 - Java

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.

Solution 6 - Java

package - takes the compiled code and package it in its distributable format, such as a JAR or WAR file. install - install the package into the local repository, for use as a dependency in other projects locally

Solution 7 - Java

The proper way is mvn package if you did things correctly for the core part of your build then there should be no need to install your packages in the local repository.

In addition if you use Travis you can "cache" your dependencies because it will not touch your $HOME.m2/repository if you use package for your own project.

In practicality if you even attempt to do a mvn site you usually need to do a mvn install before. There's just too many bugs with either site or it's numerous poorly maintained plugins.

Solution 8 - Java

It depends on what you're trying to achieve after changing the Java file. Until you want to test the maven process, you never need to do anything. Eclipse/MyEclipse will build what is necessary and put the output in the appropriate place within your project. You can also run or deploy it (if it's a web project, for example), without your needing to explicitly do anything with maven. In the end, to install your project in the maven repository, you will need to do a maven install. You may also have other maven goals that you wish to execute, which MyEclipse won't do automatically.

As I say, it depends on what you want to do.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2192023View Question on Stackoverflow
Solution 1 - JavaezcodrView Answer on Stackoverflow
Solution 2 - JavaJohanView Answer on Stackoverflow
Solution 3 - JavadnaView Answer on Stackoverflow
Solution 4 - JavaMr.QView Answer on Stackoverflow
Solution 5 - Javauser1853859View Answer on Stackoverflow
Solution 6 - JavaElilmatha SivanesanView Answer on Stackoverflow
Solution 7 - JavaArchimedes TrajanoView Answer on Stackoverflow
Solution 8 - JavaTony WeddleView Answer on Stackoverflow