Maven Commands

Maven commands cheatsheet.

Build, test, package and manage dependencies — the mvn commands you use every day. Tap to copy.

Build lifecycle
mvn compileCompile the project's source code
mvn testCompile and run the unit tests
mvn packageBuild and bundle into a JAR or WAR
mvn verifyRun integration tests and quality checks
mvn installBuild and install the artifact to your local ~/.m2 repo
mvn cleanDelete the target build directory
mvn clean installClean, then build and install from scratch
Running & skipping
mvn clean install -DskipTestsBuild and install without running tests
mvn test -Dtest=<ClassName>Run a single test class
mvn exec:javaRun a main class via the exec plugin
mvn spring-boot:runRun a Spring Boot app from source
Dependencies
mvn dependency:treeShow the full dependency tree
mvn dependency:analyzeFind unused and undeclared dependencies
mvn dependency:resolveDownload and list resolved dependencies
mvn versions:display-dependency-updatesShow newer versions of your dependencies
Project info
mvn help:effective-pomPrint the fully resolved effective POM
mvn validateValidate the project is correct and complete
mvn -versionShow Maven, Java and OS versions
mvn siteGenerate the project documentation site
Profiles & options
mvn install -P <profile>Build activating a named profile
mvn install -oBuild offline without contacting remote repos
mvn install -UForce update of snapshot dependencies
mvn -pl <module> -amBuild one module and the modules it depends on

What Maven actually does

Maven is the build tool that manages the whole lifecycle of a Java project: compiling source, running tests, resolving dependencies, and packaging the result. The file at the centre of it all is pom.xml, which declares your dependencies, plugins and build settings. Maven works through a fixed sequence of phases — validate, compile, test, package, verify, install, deploy — and running any phase runs every phase before it, so a single mvn install compiles, tests, packages and installs in one go.

The build lifecycle

Understanding the phase order is the key to Maven. mvn compile builds your source into the target folder. mvn test compiles and runs your unit tests. mvn package bundles the compiled code into a JAR or WAR. mvn verify adds integration tests and quality checks, and mvn install copies the finished artifact into your local ~/.m2 repository so other projects on your machine can depend on it. Prefixing with mvn clean wipes the target directory first for a fresh build.

Skipping tests and running code

During iteration you often want to build without the full test suite: add -DskipTests to compile the tests but not run them, as in mvn clean install -DskipTests. To run just one test class use mvn test -Dtest=<ClassName>. Plugins extend Maven with runnable goals — mvn exec:java runs a main class straight from your build, and mvn spring-boot:run launches a Spring Boot application from source without packaging it first.

Dependencies and profiles

When builds go wrong it's often a dependency conflict. mvn dependency:tree prints the full resolved tree so you can spot version clashes, and mvn dependency:analyze flags unused or undeclared libraries. Profiles let one pom.xml build differently per environment — activate one with mvn install -P <profile>. These pair naturally with the Git commands for version control and the Docker commands when you containerise your app, and if you prefer the other JVM build tool see the Gradle commands.

FAQ

What's the difference between mvn install and mvn package?
mvn package builds the project and produces the JAR or WAR in the target folder. mvn install does everything package does and then copies that artifact into your local ~/.m2 repository so other projects on your machine can depend on it.
How do I skip tests in a Maven build?
Add -DskipTests to skip running tests while still compiling them, e.g. mvn clean install -DskipTests. Use -Dmaven.test.skip=true to skip compiling the tests entirely.

More cheatsheets