mvn clean package vs mvn clean install, what is the difference?
Codemia Knowledge Hub ·
The difference is one extra lifecycle phase. mvn clean package compiles your code, runs tests and creates the artifact (JAR, WAR, etc.) in target/. mvn clean install does all of that and then copies the artifact into your local Maven repository (~/.m2/repository), so other projects on the same machine can use it as a dependency.
How the Maven default lifecycle orders phases
When you invoke a phase, Maven runs every phase before it in the sequence. The relevant part of the default lifecycle:
mvn package triggers validate, compile, test and package. mvn install triggers all of those plus verify and install, so install is a strict superset of package.
clean is a separate lifecycle. Putting it in front of either command deletes the target/ directory first, so the build starts fresh.
What mvn clean package does
- clean - Deletes
target/and all previously built files. - validate - Checks that the POM is well-formed and all required information is available.
- compile - Compiles
src/main/javainto bytecode intarget/classes. - test - Runs unit tests from
src/test/java. If any test fails, the build stops. - package - Bundles compiled classes and resources into a JAR, WAR or EAR in
target/.
Afterward the artifact exists only in the project's own target/ directory. No other project can reference it as a Maven dependency.
What mvn clean install adds
This runs every phase package runs, plus two more:
- verify - Runs integration tests and checks that quality criteria are met.
- install - Copies the packaged artifact and its POM into
~/.m2/repository.
Any other Maven project on the same machine can then declare the artifact as a <dependency>, and Maven resolves it from the local repository without a remote fetch.
| Phase | mvn clean package | mvn clean install |
Deletes target/ | Yes | Yes |
| Compiles source code | Yes | Yes |
| Runs unit tests | Yes | Yes |
Creates JAR/WAR in target/ | Yes | Yes |
| Runs integration tests (verify) | No | Yes |
Copies artifact to ~/.m2/repository | No | Yes |
| Makes artifact available to other local projects | No | Yes |
When to use package
- Standalone applications that are not a dependency of other projects.
- CI/CD pipelines where the artifact goes to a remote repository via
mvn deployinstead. - Fast feedback during development when you do not need the artifact locally.
- Building a Docker image or deployment package straight from the JAR in
target/.
When to use install
- Multi-module projects where child modules depend on sibling modules.
- Shared libraries that other local projects reference as a dependency.
- Testing an artifact from
~/.m2/repositorylocally before publishing it remotely.
Multi-module projects: where install matters most
Consider this structure:
mvn clean install from the parent builds each module in dependency order and installs each artifact locally, so api-module can resolve core-module during compilation.
mvn clean package from the parent resolves inter-module dependencies through the reactor within the same build session. That works for a full build from the parent, but fails if you later build api-module on its own:
Running mvn clean install from the parent first avoids this.
Skipping tests for faster iteration
Both commands run the full test suite by default. To skip tests temporarily:
Use -DskipTests sparingly. It speeds up builds but hides regressions.
Mistakes with package, install and deploy
Running install on every build. Defaulting to mvn clean install for standalone applications fills ~/.m2/repository with intermediate SNAPSHOT versions and can cause stale artifact issues when switching branches.
Leaving out clean. mvn package without clean can produce artifacts containing stale class files from an earlier compilation. Include clean unless you are sure the incremental build is correct.
Confusing install with deploy. install copies the artifact to your local machine only. To publish to a shared repository like Nexus or Artifactory, use mvn deploy.
Building one module before its dependencies are installed. A module built in isolation needs its sibling dependencies in the local repository. Run install from the parent first, or use -pl and -am to build the module together with its dependencies.
Assuming package runs integration tests. The verify phase, where integration tests typically run via the Failsafe plugin, comes after package and before install. For integration tests without installing, run mvn clean verify.
Neither package nor install publishes to a remote repository. That is the job of mvn deploy.
Related reading
- How are Spring Data repositories actually implemented?
- How can a Java program get its own process ID?
- How can create a producer using Spring Cloud Kafka Stream 3.1
- How can I access a folder inside of a resource folder from inside my jar File?
- How can I access getSupportFragmentManager in a fragment?
- How can I access static class variables within methods?
- How can I add a filter class in Spring Boot?
- How can I add business days to the current date in Java?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.