Maven commands cheatsheet.
Build, test, package and manage dependencies — the mvn commands you use every day. Tap to copy.
mvn compileCompile the project's source codemvn testCompile and run the unit testsmvn packageBuild and bundle into a JAR or WARmvn verifyRun integration tests and quality checksmvn installBuild and install the artifact to your local ~/.m2 repomvn cleanDelete the target build directorymvn clean installClean, then build and install from scratchmvn clean install -DskipTestsBuild and install without running testsmvn test -Dtest=<ClassName>Run a single test classmvn exec:javaRun a main class via the exec pluginmvn spring-boot:runRun a Spring Boot app from sourcemvn dependency:treeShow the full dependency treemvn dependency:analyzeFind unused and undeclared dependenciesmvn dependency:resolveDownload and list resolved dependenciesmvn versions:display-dependency-updatesShow newer versions of your dependenciesmvn help:effective-pomPrint the fully resolved effective POMmvn validateValidate the project is correct and completemvn -versionShow Maven, Java and OS versionsmvn siteGenerate the project documentation sitemvn install -P <profile>Build activating a named profilemvn install -oBuild offline without contacting remote reposmvn install -UForce update of snapshot dependenciesmvn -pl <module> -amBuild one module and the modules it depends onWhat 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.