Java Maven Cheat Sheet
Covers a minimal pom.xml, the most common Maven CLI commands, dependency scopes, the default build lifecycle, and multi-module projects.
2 PagesBeginnerMar 22, 2026
Minimal pom.xml
The essential elements every Maven project's POM needs.
xml
<project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.0</version> <scope>test</scope> </dependency> </dependencies></project>
Common Maven CLI Commands
The commands used day-to-day for building and inspecting a project.
bash
mvn compile # Compile source codemvn test # Run unit testsmvn package # Build a jar/war into target/mvn install # Install the artifact into the local ~/.m2 repomvn clean # Delete the target/ directorymvn clean install # Full clean rebuild + installmvn dependency:tree # Print the resolved dependency graphmvn -DskipTests package # Package without running testsmvn versions:display-dependency-updates # Check for newer dependency versions
Dependency Scopes
Control when and where a dependency is available on the classpath.
xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- default scope: compile - available everywhere --></dependency><dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> <!-- only on the test classpath --></dependency><dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <scope>provided</scope> <!-- supplied by the runtime container, not bundled --></dependency>
Build Lifecycle & Key Concepts
The phases and vocabulary behind every Maven build.
- validate to deploy- The default lifecycle runs validate, compile, test, package, verify, install, deploy in order; invoking one runs all prior phases too
- groupId:artifactId:version (GAV)- Uniquely identifies an artifact in a Maven repository
- Local repository (~/.m2)- Cache of downloaded/installed artifacts on the local machine
- Plugins- Goals like compiler:compile or surefire:test that phases bind to; configured under <build><plugins>
- Parent POM / <parent>- Lets a module inherit shared configuration and dependency versions
- <dependencyManagement>- Centralizes version numbers without adding the dependency itself, so child modules just declare groupId/artifactId
- Multi-module project- A parent pom.xml with <modules> lists sub-projects built together
Multi-Module Project Setup
A parent POM that aggregates and builds several sub-modules together.
xml
<!-- parent pom.xml --><project> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>api</module> <module>core</module> </modules></project>
Pro Tip
Run mvn dependency:tree before adding a new library to spot version conflicts early - Maven silently resolves conflicting transitive dependency versions using 'nearest wins', which can pull in an unexpected (and sometimes broken) version without any warning.
Was this cheat sheet helpful?
Explore Topics
#JavaMaven#JavaMavenCheatSheet#Programming#Beginner#MinimalPomXml#CommonMavenCLICommands#DependencyScopes#BuildLifecycleKeyConcepts#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance