Food

How to Run a Maven Clean Install (and What It Does)

By Hayride Live · Updated · 4 min read

Run mvn clean install from the root of your project and Maven will delete the old build output, compile your code, run the tests, and copy the packaged artifact (a JAR, WAR, or similar) into your local repository. That local copy is what other projects on the same machine resolve when they declare your project as a dependency. The one case where the method differs is a multi-module project: you still run the command once, from the top-level parent directory, not once per module.

The Clean Install Command, Step by Step

Open a terminal in the folder that holds your project’s top-level pom.xml, then follow these steps. Menu and label names can differ slightly depending on your operating system and terminal, but the Maven commands themselves are the same everywhere.

  1. Open a terminal (Terminal on macOS and Linux, Command Prompt or PowerShell on Windows) and change into the project root: cd path/to/your-project. The project root is the directory containing the top-level pom.xml.
  2. Confirm Maven is installed and on your path by running mvn -version. You should see a version line plus the Java version Maven is using; if the Java version is not the one your project needs, fix that before building.
  3. Run mvn clean install. The clean phase deletes the target directory; the install phase runs the full default lifecycle through package and then copies the artifact into your local repository (by default ~/.m2/repository on macOS and Linux, or C:\Users\<you>\.m2\repository on Windows).
  4. Watch the output end with BUILD SUCCESS. A typical small project finishes in under a minute; larger multi-module builds can take several minutes, mostly spent running tests.
  5. Optionally skip the tests when you need a fast local build: mvn clean install -DskipTests. This compiles test classes but does not run them, so it is not a substitute for a real test run before you share the code.

Clean, Install, and the Lifecycle Phases Between Them

Clean and install are not neighbors; install sits at the end of the default lifecycle, and every phase before it runs in order. Knowing the chain helps you read the console output and predict what a failure broke.

Phase What it does
clean Deletes the target directory so no stale classes or old JARs survive into the new build
validate Checks the project structure and POM are well formed
compile Compiles main source code into target/classes
test Runs unit tests; failures here stop the build before packaging
package Bundles compiled code into a JAR, WAR, or EAR inside target
install Copies that packaged artifact into your local .m2 repository for other local projects to use

The mvn clean install shorthand runs clean first, then the default lifecycle up through install. Running plain mvn install without clean skips the deletion step, which is faster but can leave outdated files in target if you have renamed or removed classes.

How to Tell the Build Worked

The last line of output is the quickest signal: BUILD SUCCESS followed by a total time. Above it, look for the install line naming your artifact, something like Installing /path/to/target/your-project-1.0.jar to ~/.m2/repository/.... You can also list the target directory and confirm the packaged file exists and has a fresh timestamp. For a multi-module build, the reactor summary at the end lists every module with SUCCESS or FAILURE next to it, so a partial failure is easy to spot.

Build Failures and What Each One Means

Most failed clean installs come from four places, and each fails at a different phase, which narrows the cause quickly.

Dependency resolution errors

A message like Could not resolve dependencies means Maven could not download a library, usually because of a typo in the version, a repository that no longer hosts it, or no network access. Check the <dependencies> section of the POM and, if you are behind a proxy, your Maven settings file.

Compilation errors

Failures during the compile phase point at your source code or your Java version. If the error mentions a language feature your code uses, check that mvn -version shows a JDK new enough for the project, and that the POM’s compiler settings match.

Test failures

A There are test failures message means the code compiled but tests failed. Read the report files under target/surefire-reports for the failing test names and stack traces. Fixing or intentionally skipping tests is the only way forward; the build stops before packaging.

Stale target directory confusion

Odd behavior that appears only without clean — old classes still loading, a JAR with deleted files in it — comes from leftover build output. That is exactly the problem clean exists to solve, and it is why clean install is the safe default for local builds.

Multi-Module Projects and Skipping Parts of the Build

In a multi-module (reactor) build, run mvn clean install only from the parent POM directory. Maven builds every module in dependency order, so a shared library module is installed before the module that depends on it. To rebuild only one module and its dependencies, use mvn clean install -pl module-name -am, where -pl picks the module and -am also builds anything it needs. To force fresh downloads of snapshot dependencies, add -U.

A few related questions come up often. Is mvn clean install the same as mvn install? No — the clean variant deletes target first, which takes a few extra seconds and prevents stale-file problems. Does install deploy anything? No; install only copies to your local repository. Sharing artifacts with a team requires the separate deploy goal pointed at a remote repository. Where is the built file? In the target directory of each module, alongside the copy placed in your local .m2 repository. Why does the second build run faster? Dependencies already in the local repository are not re-downloaded, and unchanged modules in a reactor build can be skipped from the command line.

Spotted a mistake? Tell us and we will correct it. How we write and check articles.