What does mvn install do?
mvn install is a Maven lifecycle command that performs all the previous build phases and then installs the generated artifact (JAR/WAR) into the local Maven repository. When we…
mvn install is a Maven lifecycle command that performs all the previous build phases and then installs the generated artifact (JAR/WAR) into the local Maven repository.
When we execute:
mvn install
Maven executes the following lifecycle phases in order:
validate
│
â–¼
compile
│
â–¼
test
│
â–¼
package
│
â–¼
verify
│
â–¼
install
Let’s understand each phase.
Step 1 – Validate
Maven first checks whether the project is valid.
It verifies things like:
- Is
pom.xmlpresent? - Is the XML syntax correct?
- Are mandatory project details available?
- Are dependencies declared properly?
If something is wrong, Maven immediately stops.
Example:
Missing artifact
BUILD FAILURE
Nothing else executes.
Step 2 – Compile
Next, Maven compiles all Java source files.
Example:
Before compilation:
src/main/java/
OrderService.java
UserController.java
After compilation:
target/classes/
OrderService.class
UserController.class
Java source code (.java) is converted into Java bytecode (.class).
Step 3 – Test
After successful compilation, Maven runs all unit tests.
It searches for test classes inside:
src/test/java
For example:
OrderServiceTest.java
UserServiceTest.java
If any test fails,
BUILD FAILURE
The JAR is not created, and the install phase never happens.
This prevents broken code from being packaged.
Step 4 – Package
If all tests pass, Maven packages the application.
For a Spring Boot application, it creates an executable JAR.
Example:
target/
order-service-1.0.jar
This JAR contains:
- Your compiled Java classes
- Spring Boot libraries
- Embedded Tomcat
- Configuration files
- Static resources
- Third-party dependencies
At this point, you can already run the application using:
java -jar target/order-service-1.0.jar
So after the package phase, your application is ready to execute.
Step 5 – Verify
The verify phase performs additional checks to ensure the packaged artifact is valid.
Depending on the project, it may execute:
- Integration tests
- Code quality checks
- Static analysis
- Security scans
- Plugin-specific validations
In many simple projects, nothing extra happens here, but Maven still executes this phase as part of the lifecycle.
Step 6 – Install
Now Maven performs the install phase.
This is the step that makes mvn install different from mvn package.
Maven copies the generated JAR into the Local Maven Repository.
Example location:
Windows
C:\Users\<username>\.m2\repository
Linux/macOS
~/.m2/repository
Suppose your project is:
Group Id
com.company
Artifact Id
order-service
Version
1.0
Maven creates:
.m2/
repository/
com/
company/
order-service/
1.0/
order-service-1.0.jar
order-service-1.0.pom
Now your project has been installed locally.
Why Does Maven Copy the JAR to .m2?
This is the most important concept.
Imagine you have two projects.
Project A
order-service
Project B
payment-service
Suppose payment-service wants to use classes from order-service.
Without mvn install, Maven cannot find the JAR.
After executing:
mvn install
The JAR becomes available inside the local repository.
Now another project can simply declare:
<dependency>
<groupId>com.company</groupId>
<artifactId>order-service</artifactId>
<version>1.0</version>
</dependency>
During the build, Maven first searches:
Local Repository (.m2)
If found,
it uses the JAR immediately.
No internet download is required.
Real-World Example
Suppose your company has three projects.
common-library
↓
order-service
↓
payment-service
The common-library contains utility classes.
You modify it.
Now you execute
mvn install
Result:
common-library.jar
↓
Installed into
↓
~/.m2/repository
Now both services automatically use the latest version.
Without installing,
they would continue using the old JAR.
Difference Between package and install
Many interviewers ask this.
Suppose you execute:
mvn package
Result:
target/
order-service.jar
The JAR exists only inside your project.
Another project cannot automatically use it.
Now execute
mvn install
Result:
target/
order-service.jar
+
.m2/
repository/
order-service.jar
Now every Maven project on your machine can use this artifact.
Visual Flow
Write Java Code
│
â–¼
mvn install
│
â–¼
Compile Source Code
│
â–¼
Run Unit Tests
│
â–¼
Create Executable JAR
│
â–¼
Copy JAR to .m2 Repository
│
â–¼
Other Maven Projects Can Use It
Interview Follow-up Question
Q. Why do we need mvn install if the JAR is already created in the target folder?
The JAR in the target folder belongs only to the current project.
Other Maven projects do not search inside another project’s target directory.
They search the Local Maven Repository (.m2/repository) for dependencies.
By running mvn install, Maven copies the JAR into the local repository, making it available as a dependency for other local projects.
Another Follow-up
Q. Does every project require mvn install?
Answer
No.
If you’re building a standalone application that no other local project depends on, mvn package is often enough.
You typically use mvn install when:
- Another local Maven project depends on your project.
- You want to test your artifact locally before publishing it.
- Your CI/CD process expects the artifact to be installed locally.
- You’re developing shared libraries or common modules.
Interview-Ready Answer (2–3 Minutes)
“
mvn installis a Maven lifecycle phase that executes all previous phases, including validate, compile, test, package, and verify, and then installs the generated artifact into the local Maven repository (.m2/repository). During the package phase, Maven creates an executable JAR or WAR. During the install phase, it copies that artifact along with its POM into the local repository using the project’sgroupId,artifactId, andversionas the directory structure. This allows other Maven projects on the same machine to declare the artifact as a dependency without manually copying the JAR. The key difference betweenmvn packageandmvn installis thatpackageonly creates the artifact in thetargetdirectory, whereasinstalladditionally makes it available in the local Maven repository for reuse by other projects.`