Maven Build Tool Commands & pom.xml Cheatsheet 2026
Essential guide to Maven, managing the build lifecycle, compiling, testing, packaging, dependency resolution, and common pom.xml definitions.
Build Lifecycle
When to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn cleanOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn compileOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn testOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn packageOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn installOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn deployOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sDependencies
When to Use
When analyzing transitive library trees, identifying duplicate dependencies, or resolving conflict layers.
Common Mistakes
Including redundant libraries or overlapping versions, inflating the compiled package size.
Shortcut / Pro-Tip
Use 'mvn dependency:tree' to view exactly which transitive dependency pulled in a conflicting version.Example
mvn dependency:treeOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When analyzing transitive library trees, identifying duplicate dependencies, or resolving conflict layers.
Common Mistakes
Including redundant libraries or overlapping versions, inflating the compiled package size.
Shortcut / Pro-Tip
Use 'mvn dependency:tree' to view exactly which transitive dependency pulled in a conflicting version.Example
mvn dependency:analyzeOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When analyzing transitive library trees, identifying duplicate dependencies, or resolving conflict layers.
Common Mistakes
Including redundant libraries or overlapping versions, inflating the compiled package size.
Shortcut / Pro-Tip
Use 'mvn dependency:tree' to view exactly which transitive dependency pulled in a conflicting version.Example
mvn dependency:resolveOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sPlugins & Execution
When to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn exec:java -Dexec.mainClass="com.example.App"Output Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn spring-boot:runOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sTroubleshooting
When to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn clean install -DskipTestsOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn clean install -UOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sWhen to Use
When structuring, compiling, executing unit tests, resolving dependencies, and packaging Java projects into runnable JAR or WAR files.
Common Mistakes
Hardcoding dependency versions across multiple modules instead of declaring them centrally inside parent pom.xml dependencyManagement blocks.
Shortcut / Pro-Tip
Run Maven commands with '-DskipTests' to build, package, or install artifacts rapidly when offline or in local loops.Example
mvn -XOutput Example
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 sMaven Best Practices
1Centralize Versions with Properties
Declare dependency version numbers inside custom XML tags in the properties section of pom.xml to update them in a single place.
2Avoid Hardcoded Local Repository Paths
Never hardcode paths to local dependencies. Publish them to a local repository (mvn install) or configure custom repository servers.
3Structure Multi-Module Projects
For large applications, split features into separate maven sub-modules controlled by a root aggregator POM for modularity.
4Audit Dependencies with dependency:analyze
Run analysis plugins regularly to prune unused dependencies and declare missing direct imports.
5Lock Down Plugin Versions
Always specify explicit versions for plugins in the pluginManagement section to prevent upstream updates from breaking your builds.
Common Maven Errors & Solutions
Non-resolvable parent POM
Maven cannot locate the declared parent pom.xml file. Solution: Ensure the relativePath tag points to the correct location or build parent modules first.
DependencyResolutionException / ArtifactNotFoundException
Maven failed to download a dependency from remote repositories. Solution: Verify coordinate spellings and network proxy configurations.
Fatal error compiling: invalid target release
The target JDK version configured in pom.xml is higher than your system's installed JDK. Solution: Match maven.compiler.target properties to your active Java environment.
PluginExecutionException
A build plugin crashed during execution. Solution: Run 'mvn <phase> -e -X' to inspect full plugin exception logs.
Duplicate class definitions (Classpath collision)
Two dependencies contain identical classes, causing runtime conflicts. Solution: Exclude the conflicting jar inside the offending dependency tag.
Common Maven Interview Questions
Q1What is Maven and how does it manage build processes?
Maven is a Java build automation and project management tool. It uses a project object model (POM) represented by a pom.xml file to manage dependencies, plugins, and the build lifecycle.
Q2What are the three main build lifecycles in Maven?
The three build lifecycles are: 'default' (handles compilation, testing, and deployment), 'clean' (handles project cleaning), and 'site' (handles project documentation creation).
Q3What is the difference between Maven compile, package, and install phases?
'compile' compiles source code. 'package' takes compiled classes and bundles them into a format like JAR. 'install' copies the packaged jar file into your local repository (~/.m2) so other local projects can reference it.
Q4How does dependency transitive resolution work in Maven?
If Project A depends on Project B, and Project B depends on Project C, Maven automatically resolves and downloads Project C as a transitive dependency of Project A.
Q5What is the purpose of dependencyManagement in a parent POM?
dependencyManagement allows centralizing and declaring dependency versions in a parent POM. Sub-modules can then declare dependencies without specifying versions, ensuring absolute consistency.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com