mvn package → Doesn’t install locally vs mvn install → Installs locally
This is another concept that confuses many developers. Let's make it crystal clear. When people say: mvn package → Doesn't install locally mvn install → Installs locally they…
This is another concept that confuses many developers. Let’s make it crystal clear.
When people say:
mvn package→ Doesn’t install locallymvn install→ Installs locally
they are NOT talking about installing the application on your computer.
They mean installing the artifact into Maven’s Local Repository (.m2/repository).
First understand the two locations
After building a project, there are two possible places where the JAR can exist.
Location 1 – Project’s target folder
Order-Service/
│
├── src/
├── pom.xml
└── target/
└── order-service-1.0.jar
This JAR belongs only to this project.
Location 2 – Maven Local Repository
C:\Users\Prakash\.m2\repository
or
~/.m2/repository
Example
.m2/
repository/
com/
company/
order-service/
1.0/
order-service-1.0.jar
This JAR can now be used by any Maven project on your machine.
What happens with mvn package?
Suppose you execute
mvn package
Flow
Compile
↓
Test
↓
Package
Result
Order-Service/
target/
order-service-1.0.jar
Notice
.m2/
repository/
does NOT get updated.
The JAR only exists inside your project.
Think of it like this:
Order-Service
↓
Own Locker
↓
target/
Nobody else can automatically use it.
What happens with mvn install?
Now execute
mvn install
Flow
Compile
↓
Test
↓
Package
↓
Install
Now Maven does
Step 1
Creates
target/order-service.jar
Exactly like package.
Step 2
Copies
target/order-service.jar
↓
to
~/.m2/repository/
Now there are two copies.
Project
target/
order-service.jar
AND
.m2/
repository/
order-service.jar
Why is .m2 important?
Imagine you have two projects.
Order-Service
and
Payment-Service
Payment-Service needs Order-Service.
In pom.xml
<dependency>
<groupId>com.company</groupId>
<artifactId>order-service</artifactId>
<version>1.0</version>
</dependency>
Now when Maven builds Payment-Service,
it searches
1. Local Repository (.m2)
↓
2. Remote Repository (Maven Central)
If it finds
order-service.jar
inside .m2
it immediately uses it.
Without mvn install, Maven won’t find your locally built artifact unless it’s available from a remote repository.
Real Company Example
Suppose your company has
Common-Library
Order-Service
Payment-Service
Both services depend on
Common-Library
You modify
Common-Library
Now execute
mvn install
Result
Common-Library.jar
↓
.m2/repository
Now
Order-Service
and
Payment-Service
can immediately use the latest version.
No manual copying.
No uploading.
Why is it called “Install”?
Think of it like installing software.
When you install Chrome,
it is copied from
Downloads
↓
to
Program Files
Similarly
target/order-service.jar
↓
is copied into
.m2/repository
So Maven says
“The artifact has been installed into the local repository.”
Visual Comparison
mvn package
Write Code
│
▼
Compile
│
▼
Package
│
▼
target/order-service.jar
Done
Only one copy.
mvn install
Write Code
│
▼
Compile
│
▼
Package
│
▼
target/order-service.jar
│
▼
Copy JAR
│
▼
~/.m2/repository/order-service.jar
Two copies.
Interview Question
What does “Installs locally” mean in Maven?
“Installing locally means Maven copies the generated artifact (JAR or WAR) from the project’s
targetdirectory into the local Maven repository (~/.m2/repository). This makes the artifact available as a dependency for other Maven projects on the same machine. It does not mean installing or running the application on the operating system.”
Easy Memory Trick
Think of it like this:
mvn package
↓
Makes the JAR
mvn install
↓
Makes the JAR
+
Registers it in Maven's local repository
One-line interview answer
mvn package→ “Build my application.”mvn install→ “Build my application and make it available to other local Maven projects by copying it into the local Maven repository (.m2/repository).”