The POM: Coordinates & Conventions
Every Maven project is described by one file, pom.xml: the Project Object Model. It is declarative: you don't tell Maven how to compile, you tell it what the project is, and Maven's conventions and plugins do the rest. A POM for a plain library can be ten lines long, because almost everything has a default.
Coordinates: the name of an artifact
Every artifact in the Maven world (your project, every library, every plugin) is identified by coordinates:
| Element | Meaning | Example |
|---|---|---|
groupId |
the organisation or project family, usually a reversed domain | org.apache.commons |
artifactId |
the module's name | commons-lang3 |
version |
its version; -SNAPSHOT marks a development version |
3.21.0, 1.0-SNAPSHOT |
packaging |
the kind of artifact, default jar |
jar, pom, war |
Written in one line: org.apache.commons:commons-lang3:3.21.0. Coordinates decide where the artifact lives in a repository (org/apache/commons/commons-lang3/3.21.0/commons-lang3-3.21.0.jar) and are how other projects depend on it. Gradle uses the very same coordinates.
Starting from an archetype
An archetype is a project template. maven-archetype-quickstart makes a small JAR project with one class and one JUnit test:
mvn -q -o archetype:generate -DinteractiveMode=false \
-DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 \
-DgroupId=com.example -DartifactId=quick -Dversion=1.0-SNAPSHOT
cd quick
find . -type f | sort
grep -E '<(groupId|artifactId|version|maven.compiler.release)>' pom.xml | head -4
mvn -q package
ls target./.mvn/jvm.config
./.mvn/maven.config
./pom.xml
./src/main/java/com/example/App.java
./src/test/java/com/example/AppTest.java
<groupId>com.example</groupId>
<artifactId>quick</artifactId>
<version>1.0-SNAPSHOT</version>
<maven.compiler.release>17</maven.compiler.release>
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
quick-1.0-SNAPSHOT.jar
surefire-reports
test-classes
The archetype also wrote a .mvn/ folder (per-project JVM and Maven options, empty here). The generated POM is longer than it needs to be: it pins a version for every default plugin (you'll see why that's good practice in Plugins), and its JUnit and Java versions are a little old. Real projects usually start from a company template, not the archetype. -o (offline) only stops Maven from looking for a newer archetype catalog online.
The minimal POM and the Super POM
Everything you don't write comes from the Super POM, a POM built into Maven that every project inherits from. It defines the standard folder layout, the build directory, the default repository (Maven Central) and the default plugin versions. mvn help:evaluate shows the value of any expression after inheritance:
mkdir -p tiny && cd tiny
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>tiny</artifactId>
<version>0.1.0</version>
</project>
POM
for e in project.packaging project.build.sourceDirectory project.build.testSourceDirectory \
project.build.directory project.build.outputDirectory project.build.finalName; do
printf '%-34s %s\n' "$e" "$(mvn -q help:evaluate -Dexpression=$e -DforceStdout)"
doneproject.packaging jar
project.build.sourceDirectory /home/learner/tiny/src/main/java
project.build.testSourceDirectory /home/learner/tiny/src/test/java
project.build.directory /home/learner/tiny/target
project.build.outputDirectory /home/learner/tiny/target/classes
project.build.finalName tiny-0.1.0
That is the standard directory layout:
| Folder | Holds |
|---|---|
src/main/java |
production sources |
src/main/resources |
files copied into the JAR (configuration, templates) |
src/test/java, src/test/resources |
tests and their files, never shipped |
target/ |
everything Maven produces; mvn clean deletes it |
Follow the layout and you configure nothing. Fight it (<sourceDirectory>src</sourceDirectory>) and every plugin, IDE and colleague has to be told.
The sections of a POM
A full POM has a predictable shape. You'll meet every section in this course:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion> <!-- always 4.0.0 for Maven 3 -->
<parent>…</parent> <!-- inherit from another POM -->
<groupId>…</groupId> <artifactId>…</artifactId> <version>…</version>
<packaging>jar</packaging>
<properties>…</properties> <!-- ${name} values, plugin settings -->
<dependencyManagement>…</dependencyManagement> <!-- versions for the whole tree -->
<dependencies>…</dependencies> <!-- what this module uses -->
<build>
<pluginManagement>…</pluginManagement> <!-- plugin versions and defaults -->
<plugins>…</plugins> <!-- plugins this module runs -->
</build>
<modules>…</modules> <!-- sub-projects (packaging pom) -->
<profiles>…</profiles> <!-- conditional changes -->
<repositories>…</repositories>
<distributionManagement>…</distributionManagement> <!-- where deploy goes -->
</project>When the POM is wrong
Maven checks the model before it does anything else. Forget the version and nothing runs:
mkdir -p bad && cd bad
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>bad</artifactId>
</project>
POM
mvn validate | head -3[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] 'version' is missing. @ line 1, column 52
Tip: Properties named
maven.compiler.release,project.build.sourceEncodingandproject.build.outputTimestampare read by plugins as their defaults. Settingmaven.compiler.releaseto 21 is the one line every modern POM needs: without it, the compiler plugin uses its own default target.
Test yourself
Which three elements form an artifact's coordinates?
- name, url, version
- groupId, artifactId, version
- package, class, version
- groupId, packaging, scope
Where does a POM that sets nothing about folders get src/main/java and target/ from?
- From
settings.xml - From the Super POM that every POM inherits
- From the archetype
- Maven guesses by scanning the folder
What does the -SNAPSHOT suffix on a version mean?
- A release candidate
- A development version that can change; Maven may look for newer builds of it
- A version with sources attached
- A version built offline