Mvn install or Mvn package
Asked Answered
D

7

151

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?

Deathwatch answered 20/3, 2013 at 17:40 Comment(0)
C
178

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.

Cataract answered 20/3, 2013 at 17:56 Comment(2)
I 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 ?Naughty
@MasterJoe2 "Then, I want to deploy this into nexus" That's what deploy does. If you replace it with package, it won't be deployed to Nexus.Hux
J
47

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.

Junto answered 20/3, 2013 at 17:51 Comment(2)
I 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.Foramen
The 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.Payee
S
16

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.

Stokes answered 22/4, 2015 at 17:6 Comment(1)
The 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.Shigella
S
6

Note that if your project consists of several modules which are dependent on each other, you should use "install" instead of "package" or your build will fail, because when you use the "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.

Shon answered 29/7, 2016 at 20:48 Comment(0)
P
4

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.

Phasia answered 3/12, 2015 at 15:46 Comment(1)
I think you are talking about mvn clean deploy, not mvn clean packageSomnifacient
U
2

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.

Unifoliate answered 13/5, 2017 at 23:25 Comment(0)
F
0

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.

Fluky answered 21/3, 2013 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.