Maven Commands
Software Development
Programming Knowledge
Java Development Tools
Build Automation

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:

text
validate -> compile -> test -> package -> verify -> install -> deploy

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

bash
mvn clean package
  1. clean - Deletes target/ and all previously built files.
  2. validate - Checks that the POM is well-formed and all required information is available.
  3. compile - Compiles src/main/java into bytecode in target/classes.
  4. test - Runs unit tests from src/test/java. If any test fails, the build stops.
  5. 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

bash
mvn clean install

This runs every phase package runs, plus two more:

  1. verify - Runs integration tests and checks that quality criteria are met.
  2. 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.

xml
1<!-- In another project's pom.xml -->
2<dependency>
3    <groupId>com.example</groupId>
4    <artifactId>shared-utils</artifactId>
5    <version>1.0.0-SNAPSHOT</version>
6</dependency>
Phasemvn clean packagemvn clean install
Deletes target/YesYes
Compiles source codeYesYes
Runs unit testsYesYes
Creates JAR/WAR in target/YesYes
Runs integration tests (verify)NoYes
Copies artifact to ~/.m2/repositoryNoYes
Makes artifact available to other local projectsNoYes

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 deploy instead.
  • 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/.
bash
# CI pipeline example: package, then deploy to Nexus
mvn clean package -DskipTests
mvn deploy -DskipTests

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/repository locally before publishing it remotely.
bash
1# Install shared library, then build the consuming project
2cd ~/projects/shared-utils
3mvn clean install
4
5cd ~/projects/main-app
6mvn clean package

Multi-module projects: where install matters most

Consider this structure:

text
1parent-project/
2  pom.xml
3  core-module/
4    pom.xml
5  api-module/        (depends on core-module)
6    pom.xml
7  web-module/        (depends on api-module)
8    pom.xml

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:

bash
1# This works (reactor resolves dependencies)
2cd parent-project
3mvn clean package
4
5# This fails if core-module is not in ~/.m2/repository
6cd parent-project/api-module
7mvn clean package
8# ERROR: Could not find artifact com.example:core-module:jar:1.0.0-SNAPSHOT

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:

bash
1# Skip test execution (test classes are still compiled)
2mvn clean package -DskipTests
3
4# Skip test execution but still compile test classes
5mvn clean install -Dmaven.test.skip=false -DskipTests
6
7# Skip everything including test compilation
8mvn clean package -Dmaven.test.skip=true

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.

bash
# Build api-module and all modules it depends on
mvn clean install -pl api-module -am

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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms