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.
Apache Maven is a widely-used automation tool designed for Java projects, employing a Project Object Model (POM) to manage project's build, reporting, and documentation. Among its most common commands are mvn clean package and mvn clean install, which though seemingly similar, serve distinct purposes and perform different tasks in the build lifecycle.
Understanding Maven Lifecycles:
Maven is built around the concept of lifecycle phases. There are three standard lifecycles: default, clean, and site. The clean lifecycle handles project cleaning, the default lifecycle handles project deployment, and the site lifecycle manages the creation of the project's site documentation.
mvn clean: This command triggers thecleanlifecycle to clean the project by removing files generated during previous builds (typically found in thetargetdirectory).mvn package: This command belongs to the default lifecycle. It compiles the project code and packages the compiled code into its distributable format, such as a JAR or WAR.mvn install: Also part of the default lifecycle, it not only packages the project but additionally installs it into the local Maven repository, making it accessible for other projects using this repository as a dependency source.
Describing mvn clean package:
When you run the command mvn clean package, Maven executes two major tasks:
- Cleaning the project: It first removes the
targetdirectory to ensure that there are no residuary files from previous builds that might affect the new package. - Packaging the project: After a clean state is assured, Maven compiles the source code of the project and packages the binaries (e.g.,
.jar,.war) including any resources and dependencies into thetargetdirectory.
This command is useful when you need to generate a project package that can be tested in environments like Continuous Integration (CI) servers without installing it to your local repository.
Understanding mvn clean install:
The mvn clean install command, on the other hand, is used primarily when you want to place your build artifacts in the local Maven repository after the build. It includes the following steps:
- Cleaning the project: Similar to
mvn clean package, it removes thetargetdirectory. - Packaging the project: Compiles and packages the project as in the previous command.
- Installing the package locally: The final packaged artifact (like a
jarorwarfile) will then be copied into the local Maven repository. This allows other projects on the same machine to reference it as a dependency without needing to fetch from a remote repository.
This command is particularly useful during development when you need your project to be locally available as a dependency for other projects.
Comparison Table:
| Feature | mvn clean package | mvn clean install |
| Project Cleaning | Removes the target directory | Removes the target directory |
| Compilation | Compiles project source files | Compiles project source files |
| Packaging | Creates project artifact | Creates project artifact |
| Local Installation | Does not install the artifact | Installs the artifact in local repository |
Conclusion:
Each Maven command is tailored for different stages of the development and deployment cycle. Knowing when to use mvn clean package versus mvn clean install significantly enhances build management efficiency, ensuring artifacts are generated and distributed correctly throughout the development lifecycle. For instance, use mvn clean package during development to ensure your builds are compiled and packaged correctly without overpopulating your local repository. Conversely, mvn clean install is ideal when the various projects are interdependent or to ensure all local alterations are available to other local projects.

