The mvn install
command is used in Apache Maven to compile, test, and package your project, and then install the resulting JAR or WAR file into the local Maven repository on your machine. This allows other Maven projects on the same machine to reference this artifact as a dependency.
Steps Maven Executes During mvn install
- Validate: Checks if the project is correctly configured and all necessary information is available.
- Compile: Compiles the source code of the project.
- Test: Runs the unit tests to ensure the code is functioning as expected.
- Package: Packages the compiled code into a JAR, WAR, or other distributable format as defined in the
pom.xml
. - Install: Copies the package to the local Maven repository (usually located at
~/.m2/repository
).
Typical Usage
Run the following command in the root directory of your Maven project:
mvn install
Common Use Cases
-
Local Dependency Sharing: When you're working on multiple Maven projects, you can use
mvn install
on one project so others can reference it as a dependency.<dependency> <groupId>com.example</groupId> <artifactId>my-library</artifactId> <version>1.0.0</version> </dependency>
-
Build Artifacts: Use this command to generate final build artifacts (e.g., JAR, WAR) that can be deployed.
Notes
- If the build fails during any of the phases, Maven will stop the process and report the errors.
- Ensure all tests pass before using
mvn install
, or use-DskipTests
to skip tests:mvn install -DskipTests
- To clean the previous build artifacts before installing, you can use:
mvn clean install