Build Tools
Updated for 2026

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

mvn clean
Delete the 'target' directory where compiled classes and package files are generated.

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

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn compile
Compile the source code files (.java) of the project.

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 compile

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn test
Run all unit tests (such as JUnit) inside the test source folder.

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 test

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn package
Compile and package the compiled code into its distributable format (e.g., JAR or WAR).

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 package

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn install
Package and install the artifact into the local Maven repository (~/.m2) for other projects.

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 install

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn deploy
Copy the final packaged artifact to the remote repository for sharing.

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 deploy

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s

Dependencies

mvn dependency:tree
Generate a hierarchical tree of dependencies, helping identify version conflicts.

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:tree

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn dependency:analyze
Analyze dependencies to find unused declared dependencies and used undeclared dependencies.

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:analyze

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn dependency:resolve
Download and resolve all dependencies listed in your pom.xml file.

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:resolve

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s

Plugins & Execution

mvn exec:java -Dexec.mainClass="com.example.App"
Execute a specific Java main class directly via Maven exec plugin.

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

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn spring-boot:run
Launch a Spring Boot application directly using the Spring Boot Maven plugin.

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 spring-boot:run

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s

Troubleshooting

mvn clean install -DskipTests
Build and install the project locally while skipping the execution of all unit tests.

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 -DskipTests

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn clean install -U
Force-update and download any snapshot dependencies from remote repositories.

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 -U

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s
mvn -X
Run Maven commands in full debug (verbose) mode to inspect errors.

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 -X

Output Example

Console / Terminal
[INFO] Scanning for projects...
[INFO] BUILD SUCCESS
[INFO] Total time: 5.432 s

Maven 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

Error

Non-resolvable parent POM

Solution

Maven cannot locate the declared parent pom.xml file. Solution: Ensure the relativePath tag points to the correct location or build parent modules first.

Error

DependencyResolutionException / ArtifactNotFoundException

Solution

Maven failed to download a dependency from remote repositories. Solution: Verify coordinate spellings and network proxy configurations.

Error

Fatal error compiling: invalid target release

Solution

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.

Error

PluginExecutionException

Solution

A build plugin crashed during execution. Solution: Run 'mvn <phase> -e -X' to inspect full plugin exception logs.

Error

Duplicate class definitions (Classpath collision)

Solution

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.