How are "mvn clean package" and "mvn clean install" different?

JavaMaven

Java Problem Overview


What exactly are the differences between mvn clean package and mvn clean install? When I run both of these commands, they both seem to do the same thing.

Java Solutions


Solution 1 - Java

Well, both will clean. That means they'll remove the target folder. The real question is what's the difference between package and install?

package will compile your code and also package it. For example, if your pom says the project is a jar, it will create a jar for you when you package it and put it somewhere in the target directory (by default).

install will compile and package, but it will also put the package in your local repository. This will make it so other projects can refer to it and grab it from your local repository.

Documentation

Solution 2 - Java

What clean does (common in both the commands) - removes all files generated by the previous build


Coming to the difference between the commands package and install, you first need to understand the lifecycle of a maven project


These are the default life cycle phases in maven

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

How Maven works is, if you run a command for any of the lifecycle phases, it executes each default life cycle phase in order, before executing the command itself.

order of execution

> validate >> compile >> test (optional) >> package >> verify >> install >> deploy

So when you run the command mvn package, it runs the commands for all lifecycle phases till package > validate >> compile >> test (optional) >> package

And as for mvn install, it runs the commands for all lifecycle phases till install, which includes package as well > validate >> compile >> test (optional) >> package >> verify >> install


So, effectively what it means is, install commands does everything that package command does and some more (install the package into the local repository, for use as a dependency in other projects locally)

Source: Maven lifecycle reference

Solution 3 - Java

package will generate Jar/war as per POM file. install will install generated jar file to the local repository for other dependencies if any.

install phase comes after package phase

Solution 4 - Java

package will add packaged jar or war to your target folder, We can check it when, we empty the target folder (using mvn clean) and then run mvn package.
install will do all the things that package does, additionally it will add packaged jar or war in local repository as well. We can confirm it by checking in your .m2 folder.

Solution 5 - Java

Package & install are various phases in maven build lifecycle. package phase will execute all phases prior to that & it will stop with packaging the project as a jar. Similarly install phase will execute all prior phases & finally install the project locally for other dependent projects.

For understanding maven build lifecycle please go through the following link https://ayolajayamaha.blogspot.in/2014/05/difference-between-mvn-clean-install.html

Solution 6 - Java

mvn package command will compile source code and also package it as a jar or war as per pom file and put it into the target folder(by default).

mvn install command will compile and package, but it will also put the package in your local repository. So that other projects can refer to it and grab it from your local repository.

mvn install command is mostly used when you wants to compile a project(library) which other projects in your repository are depending on.

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
Questionuser2388827View Question on Stackoverflow
Solution 1 - JavaDaniel KaplanView Answer on Stackoverflow
Solution 2 - JavaKetan RView Answer on Stackoverflow
Solution 3 - JavaAbdul GafoorView Answer on Stackoverflow
Solution 4 - JavaNisarg PatilView Answer on Stackoverflow
Solution 5 - JavaAarish RameshView Answer on Stackoverflow
Solution 6 - JavaABODEView Answer on Stackoverflow