How are mvn clean package and mvn clean install different?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The difference is one extra lifecycle phase. mvn clean package compiles your code, runs tests, and creates the artifact (JAR, WAR, etc.) in the target/ directory. mvn clean install does everything package does and then copies that artifact into your local Maven repository (~/.m2/repository), making it available as a dependency for other projects on the same machine.
How the Maven Default Lifecycle Works
Maven organizes work into a sequence of phases. When you invoke a phase, Maven executes every phase that comes before it in the sequence. The relevant portion of the default lifecycle looks like this:
Running mvn package triggers validate, compile, test, and package. Running mvn install triggers all of those plus verify and install. This is why install is a strict superset of package.
The clean prefix is a separate lifecycle. Adding it before either command tells Maven to delete the target/ directory first, ensuring a fresh build.
What mvn clean package Does
This command performs the following steps in order:
- clean - Deletes the
target/directory and all previously built files. - validate - Checks that the POM is well-formed and all required information is available.
- compile - Compiles the Java source files in
src/main/javainto bytecode intarget/classes. - test - Runs unit tests from
src/test/java. If any test fails, the build stops. - package - Bundles the compiled classes and resources into a distributable format (JAR, WAR, EAR) and places the artifact in
target/.
After this command completes, the artifact exists only inside the project's own target/ directory. No other project can reference it as a Maven dependency.
What mvn clean install Does
This command runs every phase that package runs, plus two additional phases:
- verify - Runs integration tests and checks to ensure quality criteria are met.
- install - Copies the packaged artifact and its POM into the local Maven repository at
~/.m2/repository.
After this command completes, any other Maven project on the same machine can declare this artifact as a <dependency> and Maven will resolve it from the local repository without needing a remote fetch.
Side-by-Side Comparison
| 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 Each Command
Use mvn clean package when:
- You are building a standalone application that does not serve as a dependency for other projects.
- You are running builds in CI/CD pipelines where the artifact is uploaded to a remote repository via
mvn deployinstead. - You want a fast feedback loop during development and do not need the artifact in your local repository.
- You are building a Docker image or deployment package from the JAR in
target/directly.
Use mvn clean install when:
- You are working on a multi-module project where child modules depend on sibling modules.
- You are developing a shared library that other local projects reference as a dependency.
- You need the artifact in
~/.m2/repositoryfor local testing before publishing to a remote repository.
Multi-Module Projects: Where install Matters Most
In a multi-module Maven project, modules often depend on each other. Consider this structure:
When you run mvn clean install from the parent, Maven builds each module in dependency order and installs each artifact into the local repository. This means api-module can resolve core-module as a dependency during compilation.
If you run mvn clean package from the parent, Maven uses the reactor to resolve inter-module dependencies within the same build session. This works for a full build from the parent, but fails if you later try to build api-module independently:
Running mvn clean install from the parent first avoids this problem.
Skipping Tests for Faster Iteration
Both commands run the full test suite by default. During rapid development, you may want to skip tests temporarily:
Use -DskipTests sparingly. It speeds up builds but hides regressions.
Common Pitfalls
Running install on every build. Developers sometimes default to mvn clean install out of habit, even for standalone applications. This pollutes ~/.m2/repository with intermediate SNAPSHOT versions and can cause stale artifact issues when switching between branches.
Forgetting clean entirely. Running mvn package without clean can produce artifacts that contain stale class files from a previous compilation. Always include clean unless you are sure the incremental build is correct.
Confusing install with deploy. The install phase copies the artifact to your local machine only. To publish to a shared repository like Nexus or Artifactory, use mvn deploy. These are different lifecycle phases with different purposes.
Building a single module without installing its dependencies first. In a multi-module project, building one module in isolation requires that its sibling dependencies exist in the local repository. Either run install from the parent first, or use the -pl and -am flags to build the module with its dependencies.
Assuming package runs integration tests. The verify phase, where integration tests typically execute via the Failsafe plugin, runs after package but before install. If you want integration tests without installing, use mvn clean verify explicitly.
Summary
mvn clean packagebuilds and tests your project, producing an artifact intarget/. It does not modify your local Maven repository.mvn clean installdoes everythingpackagedoes, plus copies the artifact to~/.m2/repositoryfor use by other local projects.- Use
packagefor standalone builds, CI pipelines, and fast iteration. Useinstallfor multi-module projects and shared libraries. - In multi-module projects,
installis usually necessary so that sibling modules can resolve each other as dependencies when built independently. - Neither command publishes to a remote repository. That is the job of
mvn deploy.

