What does mvn install in maven exactly do
Executing mvn install
performs several tasks: compiling the source code, running unit tests, packaging it into a JAR/WAR and storing it in your local Maven repository. To make it happen, just type:
This mechanism makes your project's artifacts instantly available for other local Maven-enabled projects acting as dependencies, thus, bypassing the necessity of a remote repository.
Deconstructing mvn install
When you run mvn install
, Maven goes through the build lifecycle up to the installation phase. Consequently, your project is not just compiled and packaged (into JAR or similar), but the generated artifact is registered into your local Maven repository. This repository typically resides within your user home directory (.m2/repository
), thus, providing dependencies for other local projects.
Going through the build lifecycle
The install
command aligns with Maven's build lifecycle, which covers the following phases:
validate
- Verifying the project's correctness.compile
- Processing the source code.test
- Checking the compiled source.package
- Building the code in a distributable format.verify
- Making additional checks on integration tests' results.install
- Storing the package in the local repository.
Skipping tests? Why not.
There's a way to skip tests if you want to:
Meanwhile, you can compile the tests without running them:
Going beyond the basics
Setting a custom local repository path
You can personalize the local repository path in settings.xml
if the default .m2
won't fit your needs.
Demystifying the plugin
Under the hood, the mvn install
command fires the Maven Install Plugin. The goal of this plugin, install:install
, is copying the built artifact, POM files, and any attached artifacts to the local repository. Consider it as the moving crew of your Maven world.
Turning to the web when necessary
If your project's dependencies aren't available in your local repository, Maven will try to fetch them from remote repositories. So yes, Maven can call for backup when it needs to.
Artifact availability
Once installed in the local repository, your project's artifact can hence benefit other projects on the same machine. This feature can be a real lifesaver when dealing with multiple interdependent projects.
Debugging with Maven
A deep understanding of the mvn install
process can significantly aid debugging when facing build or dependency issues. After all, who wouldn't appreciate a handy survival kit when lost in the dev wilderness?
Was this article helpful?