A parent POM and inheritance
A pom can name a parent, and everything the parent has the child inherits: the properties, the dependency versions, the plugins, the repositories. The parent is a pom with <packaging>pom</packaging>, which means it produces no file of its own; it exists to be inherited. One level of it is what almost every company project has, and the whole multi-module reactor of the next article is built on it.
The parent
A folder with two poms: the parent at the top and the child one level down. The parent holds the properties every module of a project shares, one managed dependency, and nothing that produces a jar.
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
<junit.version>5.14.3</junit.version>
<app.owner>the parent</app.owner>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
The child
The child names the parent by its three coordinates and has no group id and no version of its own: both are inherited from the parent. It declares the dependency without a version, because the parent manages it, and the dependency is the whole of its own dependency list.
child/pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>child</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>
</project>
child/src/main/java/com/example/child/Child.java
package com.example.child;
public class Child {
public static void main(String[] args) {
System.out.println("child 1.0.0");
}
}
child/src/test/java/com/example/child/ChildTest.java
package com.example.child;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class ChildTest {
@Test
void addsNothingUnexpected() {
assertEquals(3, 1 + 2);
}
}
Building the child
The child is a project of its own: mvn runs in its folder and finds the parent through ../pom.xml, which is the default relativePath of a <parent> block. The parent does not have to be installed, built or even in the same repository.
cd child
mvn -B package > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:child >--------------------------
[INFO] Building child 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ child ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ child ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[INFO]
[INFO] --- resources:3.4.0:testResources (default-testResources) @ child ---
[INFO] skip non existing resourceDirectory <project>/src/test/resources
[INFO]
[INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ child ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/test-classes
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ child ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.child.ChildTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: <TIME> s -- in com.example.child.ChildTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.5.0:jar (default-jar) @ child ---
[INFO] Building jar: <project>/target/child-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
Building child 1.0.0: the version comes from the parent, and the child pom does not have one. The compiler prints [debug release 21], which is the parent’s property, and the test compiled and ran with the JUnit version the parent manages.
What is inherited
An inherited property is readable in the child like its own, which help:evaluate shows for the one property no plugin uses:
mvn -B -q help:evaluate -Dexpression=app.owner -DforceStdout
mvn -B -q help:evaluate -Dexpression=project.version -DforceStdout
the parent
1.0.0
The list of what a child inherits, in the order it matters:
- The properties, all of them, and the child may override one by declaring it again.
- dependencyManagement, the versions of the dependencies, transitive ones included: this is the main reason a company has a parent pom.
- dependencies, the ones the parent declares outside dependencyManagement are inherited by every child, which is how a parent adds a test library to a whole project. A child that does not want one excludes it.
- build: the plugin versions, their configuration and their executions, and the resource and source folders.
- repositories, pluginRepositories and distributionManagement: where artifacts come from and where they go.
Three things are not inherited, and they are the ones a beginner expects to be: the artifactId, because it names the child; the name and the description, which are for people; and the parent’s modules, because a module list is the reactor of the pom that has it. The multi-module article shows a parent that has both a module list and children.
The effective pom of the child
The effective pom is where inheritance stops being a word and becomes a document. Run in the child, it shows the parent block, the properties it brought, the managed versions and the dependency with its version filled in. The excerpt keeps those parts and drops the repositories and the plugins:
mvn -B help:effective-pom > effective.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" effective.log
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:child >--------------------------
[INFO] Building child 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- help:3.6.0:effective-pom (default-cli) @ child ---
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:
<?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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>child</artifactId>
<version>1.0.0</version>
<properties>
<app.owner>the parent</app.owner>
<junit.version>5.14.3</junit.version>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.14.3</version>
<scope>test</scope>
The properties are alphabetically sorted in the effective pom, which is why app.owner comes first; the order of the file the project keeps does not survive. The managed dependency and the declared one are both there: one in dependencyManagement with the scope it was given, one in dependencies with that scope copied onto it. Nothing says which pom each line came from, and that is the point of the document: after inheritance there is one pom.
A parent that is not next door
relativePath is a convenience for a parent that sits above the child in the same checkout. A child whose parent is a published pom says so, and the resolver finds the parent in the local repository like any artifact:
The block keeps the three coordinates and adds an empty <relativePath/>; a child that leaves the element out takes the default, ../pom.xml.
An empty <relativePath/> turns the file lookup off, and then the parent must be installed or downloaded before the child can be read. That is the shape every project outside its own repository has, and it is the failure a fresh checkout meets when the parent is not published yet: Non-resolvable parent POM, which the errors article shows next to the other two.
Where to go next
The multi-module reactor puts three of these children under one parent and builds them in one command, in the order their dependencies demand.
Article author: Arthur Isaev