The POM and the mvn command

Maven · Essentials · 10 min

The POM is the single source of truth for a Maven build; every task starts from it and from the mvn command.

Maven describes a project in pom.xml, the Project Object Model. Every project has coordinates:

  • groupId: the organization or project namespace, like com.acme.
  • artifactId: the name of the module, like shop-api.
  • version: 1.0.0 for a release or 1.1.0-SNAPSHOT for work in progress.
  • packaging: jar (default), war, or pom for aggregator/parent projects.

The same coordinates identify every library you depend on. Maven follows convention over configuration: sources live in src/main/java, resources in src/main/resources, tests in src/test/java, and output goes to target/. You configure only what differs from the conventions.

Set the Java version with the maven.compiler.release property (<properties><maven.compiler.release>21</maven.compiler.release></properties>), and project.build.sourceEncoding to UTF-8.

Common commands:

  • mvn compile, mvn test, mvn package (builds target/shop-api-1.0.0.jar), mvn verify, mvn install (copies to ~/.m2/repository).
  • mvn clean deletes target/; mvn clean package is the typical full build.
  • -q quiet, -X debug, -e show stack traces, -DskipTests skip tests, -o offline, -f other/pom.xml another file.

Most projects commit the Maven Wrapper (mvnw, .mvn/wrapper/) created with mvn wrapper:wrapper, so everyone uses the same Maven version; run ./mvnw instead of mvn.

Example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.acme</groupId>
  <artifactId>shop-api</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

<!-- src/main/java/com/acme/App.java, then:
     ./mvnw clean package        -> target/shop-api-1.0.0-SNAPSHOT.jar
     ./mvnw test -q              -> runs src/test/java with Surefire
     java -cp target/classes com.acme.App -->

Remember

  • groupId:artifactId:version identify a project and every dependency
  • Conventions: src/main/java, src/test/java, target/
  • maven.compiler.release sets the Java version
  • clean, compile, test, package, install are the everyday commands

Watch out for

  • Running mvn with a different major version than teammates; commit the wrapper and use ./mvnw.

Check yourself

  1. What does mvn install do beyond mvn package?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: maven.apache.org/guides/introduction/introduction-to-the-pom.html