Maven - Commands

Cheatsheet Maven

Essential Maven commands for building, testing, and managing Java projects

Build - Clean Install

Clean and build project with dependencies installed to local repository.

mvn clean install

Most commonly used Maven command. Removes target directory, compiles, tests, and installs to ~/.m2/repository.

Build - Clean Package

Clean and package project without installing to local repository.

mvn clean package

Faster than install when you don’t need artifacts in local repo. Creates JAR/WAR in target/.

Build - Compile Only

Compile source code without running tests.

mvn compile

Compiles src/main/java to target/classes. Useful for quick syntax checks.

Build - Skip Tests

Build project while skipping test execution.

mvn clean install -DskipTests

Compiles tests but doesn’t run them. Faster builds when tests are not needed.

Skip test compilation entirely:

mvn clean install -Dmaven.test.skip=true

Test - Run All Tests

Execute all unit tests in the project.

mvn test

Runs tests in src/test/java. Generates reports in target/surefire-reports/.

Test - Run Specific Test

Run a single test class.

mvn test -Dtest=UserServiceTest

Run specific test method:

mvn test -Dtest=UserServiceTest#testLogin

Test - Run Pattern

Run tests matching a pattern.

mvn test -Dtest=*ServiceTest

Matches all test classes ending with ServiceTest.

Dependency - Show Tree

Display project dependency tree.

mvn dependency:tree

Shows all direct and transitive dependencies in tree format.

Filter by groupId:

mvn dependency:tree -Dincludes=org.springframework

Dependency - Analyze

Find unused and undeclared dependencies.

mvn dependency:analyze

Helps identify:

  • Unused declared dependencies
  • Used undeclared dependencies (should be in POM)

Dependency - Copy Dependencies

Copy all dependencies to target directory.

mvn dependency:copy-dependencies

Copies JARs to target/dependency/. Useful for creating lib folders.

Specify output directory:

mvn dependency:copy-dependencies -DoutputDirectory=lib

Dependency - Resolve

Download all dependencies to local repository.

mvn dependency:resolve

Ensures all dependencies are in ~/.m2/repository without building.

Clean - Remove Target

Delete the target directory.

mvn clean

Removes all compiled classes, JARs, and build artifacts from target/.

Package - Create JAR

Package compiled code into JAR file.

mvn package

Creates JAR in target/<artifactId>-<version>.jar.

Package - Create Executable JAR

Build JAR with dependencies included (fat JAR).

mvn clean package assembly:single

Requires maven-assembly-plugin. Creates executable JAR with all dependencies.

Install - Local Repository

Install artifact to local Maven repository.

mvn install

Installs to ~/.m2/repository for use by other local projects.

Install - Custom JAR

Install external JAR to local repository.

mvn install:install-file \
  -Dfile=mylib.jar \
  -DgroupId=com.example \
  -DartifactId=mylib \
  -Dversion=1.0 \
  -Dpackaging=jar

Deploy - Remote Repository

Deploy artifact to remote repository.

mvn deploy

Uploads to repository configured in <distributionManagement>. Requires credentials in settings.xml.

Version - Display Version

Show Maven version and Java version.

mvn -version

Displays Maven version, Java version, and OS information.

Version - Update Parent

Update parent POM version.

mvn versions:update-parent

Updates <parent> version to latest release.

Version - Set Version

Set new project version.

mvn versions:set -DnewVersion=2.0.0

Updates version in POM and all modules.

Commit the change:

mvn versions:commit

Revert the change:

mvn versions:revert

Plugin - Execute Goal

Run specific plugin goal.

mvn groupId:artifactId:version:goal

Example - Spring Boot run:

mvn spring-boot:run

Plugin - List Plugins

Display all plugins in the project.

mvn help:effective-pom | grep plugin

Lifecycle - Validate

Validate project structure and POM.

mvn validate

Checks if project is correct and all necessary information is available.

Lifecycle - Verify

Run integration tests.

mvn verify

Runs unit tests and integration tests. Higher lifecycle phase than test.

POM - Effective POM

Display effective POM with all inherited settings.

mvn help:effective-pom

Shows final POM after inheritance and interpolation.

Save to file:

mvn help:effective-pom > effective-pom.xml

POM - Effective Settings

Display effective Maven settings.

mvn help:effective-settings

Shows merged settings from global and user settings.xml.

Repository - Purge Local

Remove artifacts from local repository.

mvn dependency:purge-local-repository

Deletes project dependencies from ~/.m2/repository and re-downloads them.

Repository - Update Snapshots

Force update of snapshot dependencies.

mvn clean install -U

The -U flag forces update of snapshots and releases.

Debug - Verbose Output

Run Maven with debug information.

mvn clean install -X

Shows detailed debug logs. Useful for troubleshooting build issues.

Debug - Show Errors

Display full error stack traces.

mvn clean install -e

Shows exception stack traces when build fails.

Offline - Build Without Network

Build project in offline mode.

mvn clean install -o

Uses only local repository. Fails if dependencies are missing.

Profile - Activate Profile

Build with specific Maven profile.

mvn clean install -P production

Multiple profiles:

mvn clean install -P profile1,profile2

Multi-Module - Build Single Module

Build specific module in multi-module project.

mvn clean install -pl module-name

Build module with dependencies:

mvn clean install -pl module-name -am

Build from specific module:

mvn clean install -rf :module-name

Performance - Parallel Build

Build project using multiple threads.

mvn clean install -T 4

Uses 4 threads. Faster for multi-module projects.

Use cores:

mvn clean install -T 1C

Uses 1 thread per CPU core.

Site - Generate Documentation

Generate project documentation site.

mvn site

Creates HTML documentation in target/site/.

Archetype - Create Project

Create new Maven project from archetype.

mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=my-app \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

Source - Download Sources

Download source JARs for dependencies.

mvn dependency:sources

Download Javadocs:

mvn dependency:resolve -Dclassifier=javadoc

Wrapper - Install Maven Wrapper

Install Maven wrapper for project.

mvn wrapper:wrapper

Creates mvnw and mvnw.cmd scripts. Allows building without installing Maven globally.

Specify Maven version:

mvn wrapper:wrapper -Dmaven=3.9.6