URL: https://www.progressiverobot.com/maven-commands-options-cheat-sheet/

Introduction

Maven is one of the most popular project and dependency management tools for Java applications. Maven provides a lot of commands and options to help you in your day to day tasks.

This cheat sheet uses a sample Maven project to demonstrate some useful Maven commands. It was originally written for OpenJDK 13.0.1 and Maven 3.6.3. These commands have been verified with OpenJDK 19.0.1 and Maven 3.8.7.

Maven Commands Cheat Sheet

maven illustration for: Maven Commands Cheat Sheet

mvn clean

This command cleans the Maven project by deleting the target directory:

				
					mvn clean
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar ---
[INFO] Deleting /Users/sammy/Desktop/maven-examples/maven-example-jar/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
				
			

mvn compiler:compile

This command compiles the Java source classes of the Maven project:

				
					mvn compiler:compile
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-cli) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
				
			

mvn compiler:testCompile

This command compiles the test classes of the Maven project:

				
					mvn compiler:testCompile
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-cli) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
				
			

mvn package

This command builds the Maven project and packages it into a JAR, WAR, etc.:

				
					mvn package
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes
...
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
[INFO] Surefire report directory: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.maven.classes.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---
[INFO] Building jar: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
				
			

The output shows the location of the JAR file just before the "BUILD SUCCESS" message. Notice the package goal executes compile, testCompile, and test goals before packaging the build.

mvn install

This command builds the Maven project and installs the project files (JAR, WAR, pom.xml, etc.) to the local repository:

				
					mvn install
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-example-jar ---
...
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar ---
...
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-example-jar ---
...
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar ---
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar ---
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar ---
...
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-example-jar ---
[INFO] Installing /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar to /Users/sammy/.m2/repository/com/example/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/sammy/Desktop/maven-examples/maven-example-jar/pom.xml to /Users/sammy/.m2/repository/com/example/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
				
			

mvn deploy

This command deploys the artifact to the remote repository:

				
					mvn deploy
				
			

The remote repository should be configured properly in the project pom.xml file distributionManagement tag. The server entries in the Maven settings.xml file are used to provide authentication details.

mvn validate

This command validates the Maven project to ensure that everything is correct and all the necessary information is available:

				
					mvn validate
				
			

mvn dependency:tree

This command generates the dependency tree of the Maven project:

				
					mvn dependency:tree
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Mockito-Examples ---
[INFO] com.example.mockito:Mockito-Examples:jar:1.0-SNAPSHOT
[INFO] +- org.junit.platform:junit-platform-runner:jar:1.2.0:test
[INFO] |  +- org.apiguardian:apiguardian-api:jar:1.0.0:test
[INFO] |  +- org.junit.platform:junit-platform-launcher:jar:1.2.0:test
[INFO] |  \- org.junit.platform:junit-platform-suite-api:jar:1.2.0:test
[INFO] |     \- org.junit.platform:junit-platform-commons:jar:1.2.0:test
[INFO] +- org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
[INFO] |  +- org.junit.platform:junit-platform-engine:jar:1.2.0:test
[INFO] |  |  \- org.opentest4j:opentest4j:jar:1.1.0:test
[INFO] |  \- org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test
[INFO] +- org.mockito:mockito-junit-jupiter:jar:2.19.0:test
[INFO] |  \- org.mockito:mockito-core:jar:2.19.0:test
[INFO] |     +- net.bytebuddy:byte-buddy:jar:1.8.10:test
[INFO] |     +- net.bytebuddy:byte-buddy-agent:jar:1.8.10:test
[INFO] |     \- org.objenesis:objenesis:jar:2.6:test
[INFO] \- org.testng:testng:jar:6.14.3:test
[INFO]    +- com.beust:jcommander:jar:1.72:test
[INFO]    \- org.apache-extras.beanshell:bsh:jar:2.0b6:test
				
			

mvn dependency:analyze

This command analyzes the maven project to identify the unused declared and used undeclared dependencies:

				
					mvn dependency:analyze
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ Mockito-Examples ---
[WARNING] Used undeclared dependencies found:
[WARNING]    org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test
[WARNING]    org.mockito:mockito-core:jar:2.19.0:test
[WARNING] Unused declared dependencies found:
[WARNING]    org.junit.platform:junit-platform-runner:jar:1.2.0:test
[WARNING]    org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
[WARNING]    org.mockito:mockito-junit-jupiter:jar:2.19.0:test
				
			

It's useful in reducing the build size by identifying the unused dependencies and removing them from the pom.xml file.

mvn archetype:generate

This command generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc:

				
					mvn archetype:generate
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
...
[INFO] Project created from Archetype in dir: /Users/sammy/Desktop/maven-examples/maven-example-jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
				
			

mvn site:site

This command generates a site for the project:

				
					mvn site:site
				
			

You will notice a site directory in the target directory after executing this command.

				
					<^>/Users/sammy/Desktop/maven-examples/maven-example-jar/<^>target/site/index.html
				
			

There will be multiple HTML files inside the site directory that provide information related to the project.

<!–

–>

mvn test

This command runs the test cases of the project:

				
					mvn test
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
first-element
second-element
Employee setName Argument = Sammy
...
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
				
			

mvn compile

This command compiles the source Java classes of the project:

				
					mvn compile
				
			

Example of the output:

				
					[secondary_label Output]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ Mockito-Examples ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 10 source files to /Users/sammy/Desktop/maven-examples/Mockito-Examples/target/classes
				
			

It is similar to the previous mvn compiler:compile command, but runs the entire Maven lifecycle up to compile.

mvn verify

This command builds the project, runs all the test cases and run any checks on the results of the integration tests to ensure quality criteria are met:

				
					mvn verify
				
			

Maven Options

Maven provides a lot of command-line options to alter the Maven build process:

mvn -help

This command-line option prints the Maven usage and all the available options:

				
					mvn -help
				
			

Example of the output:

				
					[secondary_label Output]
usage: mvn [options] [&lt;goal(s)&gt;] [&lt;phase(s)&gt;]

Options:
  -am,--also-make                  If project list is specified, also
                                   build projects required by the
                                   list
  -amd,--also-make-dependents      If project list is specified, also
                                   build projects that depend on
                                   projects on the list
  -B,--batch-mode                  Run in non-interactive (batch)
                                   mode (disables output color)
  -b,--builder &lt;arg&gt;               The id of the build strategy to
                                   use
  -C,--strict-checksums            Fail the build if checksums don't
                                   match
  -c,--lax-checksums               Warn if checksums don't match
  -cpu,--check-plugin-updates      Ineffective, only kept for
                                   backward compatibility
				
			

<!–

–>

mvn -f dir/pom.xml package

This command-line option builds a project from a different location:

				
					mvn -f &lt;^&gt;dir/&lt;^&gt;pom.xml &lt;^&gt;package&lt;^&gt;
				
			

Provide the pom.xml file location to build the project. It's useful when you have to run a Maven build from a script.

mvn -o package

This command-line option runs the Maven build in _offline mode_:

				
					mvn -o &lt;^&gt;package&lt;^&gt;
				
			

It's useful when you have all the required JARs downloaded in the local repository and you don't want Maven to look for any JARs in the remote repository.

mvn -q package

This command-line option runs the Maven build in _quiet mode_, so that only the test case results and errors are displayed:

				
					mvn -q &lt;^&gt;package&lt;^&gt;
				
			

mvn -X package

This command-line option prints the Maven version and runs the build in _debug mode_, so that all messages are displayed:

				
					mvn -X &lt;^&gt;package&lt;^&gt;
				
			

Example of the output:

				
					[secondary_label Output]
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/sammy/Downloads/apache-maven-3.6.3
Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"
[DEBUG] Created new class realm maven.api
[DEBUG] Importing foreign pacakges into class realm maven.api
[DEBUG]   Imported: javax.annotation.* &lt; plexus.core
[DEBUG]   Imported: javax.annotation.security.* &lt; plexus.core
[DEBUG]   Imported: javax.enterprise.inject.* &lt; plexus.core
[DEBUG]   Imported: javax.enterprise.util.* &lt; plexus.core
[DEBUG]   Imported: javax.inject.* &lt; plexus.core
				
			

<!–

–>

mvn -v

This command-line option displays the Maven version information:

				
					mvn -v
				
			

Example of the output:

				
					[secondary_label Output]
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/sammy/Downloads/apache-maven-3.6.3
Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac"
				
			

mvn -V package

This command-line option prints the Maven version and then continues with the build:

				
					mvn -V &lt;^&gt;package&lt;^&gt;
				
			

It's equivalent to the commands:

				
					mvn -v;mvn &lt;^&gt;package&lt;^&gt;
				
			

mvn -DskipTests package

This command-line option applies the skipTests system property to skip the unit test cases from the build cycle:

				
					mvn -DskipTests &lt;^&gt;package&lt;^&gt;
				
			

You can also skip the test cases execution:

				
					mvn -Dmaven.test.skip=true &lt;^&gt;package&lt;^&gt;
				
			

mvn -T 4 clean install

This command-line option tells Maven to run parallel builds using the specified thread count:

				
					mvn -T 4 &lt;^&gt;clean&lt;^&gt; &lt;^&gt;install&lt;^&gt;
				
			

It's useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project.