M
Maven Cheat SheetQuick Reference
mvn --versionmvn -X (debug)mvn -U (force update)
Core Concepts
POM — Project Object Model, heart of Maven
GAV — groupId · artifactId · version (unique ID)
Local Repo ~/.m2/repository/
Central Repo — public Maven Central
Remote Repo — Nexus / Artifactory
Resolution order: Local → Custom Remote (pom order) → Central
Directory Layout
src/main/java ← source code
src/main/resources ← config files
src/test/java ← test code
target/ ← .class + .jar output
pom.xml ← project config
pom.xml Structure
<project>
<groupId>com.company</>
<artifactId>my-app</>
<version>1.0-SNAPSHOT</>
<packaging>jar</>
<properties> ← versions, encoding
<dependencyManagement> ← version table
<dependencies> ← actual includes
<build><plugins> ← plugins config
<profiles> ← env variants
<repositories> ← remote repos
</project>
POM Types
TypePurpose
Super POMMaven default; all projects inherit it
Parent POMShares config across modules; enforces versions
BOMVersion lookup table only; imported via scope=import
Effective POMFinal merged result (project + parent + BOM + super)
Key diff: Parent POM enforces versions; BOM suggests them. A project can have only 1 parent but multiple BOMs.
Build Lifecycle
3 Lifecycles:
cleandefaultsite
Default lifecycle phases (sequential):
1validateCheck project & info
2compile.java → .class
3testRun unit tests (Surefire)
4packageCreate JAR/WAR
5verifyRun integration tests
6installCopy to ~/.m2
7deployPush to remote repo
⚠ Phases run sequentially — can't skip earlier ones. mvn package runs 1→4.
Common Commands
CommandAction
mvn cleanDelete /target
mvn compileCompile src
mvn testRun unit tests
mvn packageBuild JAR/WAR
mvn installInstall to local repo
mvn deployDeploy to remote
mvn clean installFull clean build
mvn clean install -UForce dependency update
mvn -DskipTestsSkip test execution (compile only)
mvn -Dmaven.test.skip=trueSkip compile + execution
mvn dependency:treeShow dep tree
mvn -P prodActivate profile
mvn spring-boot:runRun Spring Boot app
mvn exec:javaRun Java program
mvn siteGenerate project site
Phase → Plugin → Goal
Phase — lifecycle stage
Goal — plugin task, format: plugin:goal
PhasePlugin : Goal bound by default
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar (or war:war)
verifyfailsafe:verify
installinstall:install
deploydeploy:deploy
cleanclean:clean
Goals can be run directly, bypassing the lifecycle:
mvn compiler:compile (no validate/initialize first)
One phase can have multiple goals bound to it (executed in declaration order).
Dependency Scopes
ScopeCompileRuntimeTestIn JAR?
compile (default)
provided
runtime
test
provided: Tomcat supplies servlet-api. runtime: JDBC drivers.
Dependency Resolution
Nearest-Wins Rule:
Your POM (L1): dep v2.0 ← wins
L2 dep needs: v1.0 ← wins over L3
L3 dep needs: v1.1 ← loses
Force a version (override Nearest):
Option A — Exclusions:
<exclusions><exclusion>
<groupId>org.slf4j</>
</exclusion></exclusions>
Option B — dependencyManagement in Parent POM
Version conflicts? Delete ~/.m2 or run mvn clean install -U
Plugins · Goals · Phases
maven-compiler-plugincompile · test-compile
GoalPhaseDoesDirect cmd
compiler:compilecompileCompiles src/main/javamvn compiler:compile
compiler:testCompiletest-compileCompiles src/test/javamvn compiler:testCompile
<configuration>
<source>17</source> <target>17</target>
</configuration>
maven-surefire-plugintest
GoalPhaseDoesDirect cmd
surefire:testtestRuns JUnit / TestNG unit tests, generates XML/txt reports in target/surefire-reportsmvn surefire:test
← parallel execution config:
<parallel>methods</> <threadCount>10</>
maven-failsafe-pluginintegration-test · verify
GoalPhaseDoesDirect cmd
failsafe:integration-testintegration-testRuns *IT.java testsmvn failsafe:integration-test
failsafe:verifyverifyChecks if IT tests passed; fails build here (not mid-test)mvn failsafe:verify
⚠ Always need both goals. Run with mvn verify
Packaging pluginspackage
Plugin : GoalPhaseOutputDirect cmd
jar:jarpackageThin JAR (no deps)mvn jar:jar
shade:shadepackageFat/Uber JAR (all deps merged)mvn package
assembly:singlepackageCustom zip/tar/dir distributionmvn assembly:single
war:warpackageWAR file (web apps)mvn war:war
maven-dependency-pluginno default phase
GoalDoesDirect cmd
dependency:treePrint full dependency treemvn dependency:tree
dependency:analyzeFind unused/undeclared depsmvn dependency:analyze
dependency:copy-dependenciesCopy all dep JARs to a foldermvn dependency:copy-dependencies
dependency:unpackExtract a dep's contentsmvn dependency:unpack
spring-boot-maven-pluginpackage / no default
GoalPhaseDoesDirect cmd
spring-boot:repackagepackageRewraps JAR into executable fat JARmvn package
spring-boot:runStart app without building JARmvn spring-boot:run
spring-boot:build-imageBuild OCI/Docker image (Buildpacks)mvn spring-boot:build-image
Plugin : GoalPhaseDoesDirect cmd
exec:javaRun main() class directlymvn exec:java
exec:execRun any shell commandmvn exec:exec
clean:cleancleanDelete /target dirmvn clean:clean
install:installinstallCopy artifact to ~/.m2mvn install:install
deploy:deploydeployPush artifact to remote repomvn deploy:deploy
release:prepareStrip -SNAPSHOT, tag in Gitmvn release:prepare
release:performBuild & deploy tagged releasemvn release:perform
versions:setUpdate version in pom.xmlmvn versions:set -DnewVersion=2.0
Custom Goal→Phase Binding (executions):
← bind dependency:copy-dependencies to package phase:
<executions><execution>
<id>copy-deps</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution></executions>
Any goal can be re-bound to any phase using <executions>. Multiple executions of the same plugin are allowed.
Maven Profiles
Different config per environment (Dev/Test/Prod):
<profiles><profile>
<id>prod</id>
<properties>
<db.url>jdbc:postgresql://...</>
</properties>
<activation><activeByDefault>false</></>
</profile></profiles>
mvn install -P prodmvn install -P dev
Also use profiles to conditionally skip tests.
Multi-Module Projects
Parent POM declares modules; children inherit config:
<modules>
<module>api</module>
<module>service</module>
<module>web</module>
</modules>
BOM Import pattern:
<dependencyManagement><dependencies>
<dependency>
<groupId>org.springframework.boot
<artifactId>spring-boot-dependencies
<version>3.2.0</>
<type>pom</>
<scope>import</>
</dependency>
</dependencies></dependencyManagement>
1 parent max | multiple BOMs ok | prevents inheritance bloat
Maven + Git
⚠ Never deploy uncommitted code
Artifact must map to a specific Git commit for traceability & reproducibility.
Release Flow (maven-release-plugin):
1.1.0.0-SNAPSHOT → 1.0.0 (remove -SNAPSHOT)
2.Commit & tag: v1.0.0 in Git
3.Deploy final artifact to remote repo
4.Bump to 1.0.1-SNAPSHOT for next dev
.gitignore (always exclude):
target/
.idea/ .classpath .project
.m2/repository (if inside project)
SNAPSHOT = in-development | RELEASE = immutable artifact
settings.xml vs pom.xml
FileControls
pom.xmlWhat project needs (deps, plugins, repos)
settings.xmlHow Maven behaves (auth credentials, mirrors, local repo location, global repos)
Troubleshooting
ProblemFix
Missing artifactVerify GAV coords → mvn clean install -U
Version conflictmvn dependency:tree → add exclusion or pin in depMgmt
Corrupt local cacheDelete ~/.m2/repository or run -U
Can't find artifactCheck company Nexus/Artifactory
Debug buildmvn -X for full stack trace
Test failures blocking-DskipTests or use fast-build profile
Best Practices
Pin versions in <properties>, reference with ${var}
Use Parent POM for shared plugin config across modules
Use BOM for version consistency without inheritance
Remove unused deps → smaller JAR, faster build
Use <scope>test</scope> — keep test libs out of prod JAR
Commit before mvn deploy — ensure traceability
Use -SNAPSHOT for dev; release to remove it
Maven Build Automation · Standard Directory Layout · Convention over Configuration
mvn clean installmvn dependency:treemvn clean install -U