By Pankaj Kumar and Bradley Kouchi

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.

mvn cleanThis command cleans the Maven project by deleting the target directory:
- mvn clean
Example of the output:
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:compileThis command compiles the Java source classes of the Maven project:
- mvn compiler:compile
Example of the output:
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:testCompileThis command compiles the test classes of the Maven project:
- mvn compiler:testCompile
Example of the output:
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 packageThis command builds the Maven project and packages it into a JAR, WAR, etc.:
- mvn package
Example of the output:
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 installThis 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:
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 deployThis 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 validateThis command validates the Maven project to ensure that everything is correct and all the necessary information is available:
- mvn validate
mvn dependency:treeThis command generates the dependency tree of the Maven project:
- mvn dependency:tree
Example of the output:
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:analyzeThis command analyzes the maven project to identify the unused declared and used undeclared dependencies:
- mvn dependency:analyze
Example of the output:
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:generateThis command generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc:
- mvn archetype:generate
Example of the output:
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
Recommended Reading: Creating a Java Project using Maven Archetypes
mvn site:siteThis 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 testThis command runs the test cases of the project:
- mvn test
Example of the output:
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 compileThis command compiles the source Java classes of the project:
- mvn compile
Example of the output:
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 verifyThis 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 provides a lot of command-line options to alter the Maven build process:
mvn -helpThis command-line option prints the Maven usage and all the available options:
- mvn -help
Example of the output:
Outputusage: mvn [options] [<goal(s)>] [<phase(s)>]
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 <arg> 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 packageThis command-line option builds a project from a different location:
- mvn -f dir/pom.xml package
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 packageThis command-line option runs the Maven build in offline mode:
- mvn -o package
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 packageThis command-line option runs the Maven build in quiet mode, so that only the test case results and errors are displayed:
- mvn -q package
mvn -X packageThis command-line option prints the Maven version and runs the build in debug mode, so that all messages are displayed:
- mvn -X package
Example of the output:
OutputApache 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.* < plexus.core
[DEBUG] Imported: javax.annotation.security.* < plexus.core
[DEBUG] Imported: javax.enterprise.inject.* < plexus.core
[DEBUG] Imported: javax.enterprise.util.* < plexus.core
[DEBUG] Imported: javax.inject.* < plexus.core
mvn -vThis command-line option displays the Maven version information:
- mvn -v
Example of the output:
OutputApache 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 packageThis command-line option prints the Maven version and then continues with the build:
- mvn -V package
It’s equivalent to the commands:
- mvn -v;mvn package
mvn -DskipTests packageThis command-line option applies the skipTests system property to skip the unit test cases from the build cycle:
- mvn -DskipTests package
You can also skip the test cases execution:
- mvn -Dmaven.test.skip=true package
mvn -T 4 clean installThis command-line option tells Maven to run parallel builds using the specified thread count:
- mvn -T 4 clean install
It’s useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Hello! Excellent summary :) There is one option that should be added because of its usefulness: mvn help:effective-settings With it, you can query the settings Maven is detecting from your config, so it’s very handy when there are problems with some of them being not detected because bad writing or so
- Fer B
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.