Difference between package and install command in maven

It is a short tutorial on the Difference between the package and install commands in Maven.

You can check my other posts on maven commands.

clean, package, and install are the goal phases of the maven life cycle.

Before that first understand the maven life cycle goals on the order execution.

maven default life cycle goals execution order

Following are maven default life cycle goals execution order

  • validate: validate checks all the valid information of a maven application
  • compile: Compile source code into class files i.e java to class files which also include test classes (java and test folder java files)
  • test: Running test classes located in the src/main/test folder
  • package: package the output of compiled source code and output is JAR, WAR, EAR files.
  • verify: Checks integration test and verify the result
  • install: This installs the jar/war file into a local repository so that other projects can use these as a dependency.
  • deploy: This deploys artifact jar/war into the remote repository.

Maven clean life cycle

  • pre-clean: execute the validation before the clean goal
  • clean: delete the previous build files located in target folder
  • post-clean: execute the validation tasks after the clean goal

difference between mvn clean install and mvn clean package?

The clean goal in both commands cleans and removes previous build files. It removes the target folder of a maven project.

  • mvn clean package goal: This goal packages the project and output jar/war/ear file based on the pom module.

if your pom.xml contains package=jar, It creates a jar package if it is a war package, it generates a war package.

goal runs the below goals execution order

validate -> compile -> test -> package
  • maven clean install goal: installs the artifact to the local repository so that other projects or applications can make this a dependency and use it in their projects.

Here is an order of execution

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

install goals execution comes after the package goal.