mvn package: Creates JAR or WAR file for the project to convert it into a distributable format. mvn install: Deploys the packaged JAR/ WAR file to the local repository.
mvn clean install is the command to do just that. You are calling the mvn executable, which means you need Maven installed on your machine. (see How do you install Maven?) You are using the clean command, which will delete all previously compiled Java .
mvn clean package. Clears the target directory and Builds the project and packages the resulting JAR file into the target directory.
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. Note that mvn package will only create a jar file. mvn install will do that and install the jar (and class etc.)
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
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
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
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With