Multi-Module Reactor Builds
Real codebases are split into modules: a domain model, a persistence layer, a REST API, a CLI, test fixtures. Maven builds them together with the reactor: one command reads every module's POM, sorts them by their dependencies and builds them in that order, using freshly built modules instead of copies from the repository.
Aggregation and inheritance
A multi-module build uses two independent mechanisms, usually in the same root POM:
| Mechanism | Written as | Effect |
|---|---|---|
| Aggregation | <modules><module>core</module></modules> in a POM with packaging pom |
running Maven in the root builds all listed modules |
| Inheritance | <parent> in each module's POM |
the module inherits properties, dependencyManagement, pluginManagement, plugins |
Neither implies the other: you can aggregate modules that don't inherit from the root, and inherit from a parent that doesn't list you. In practice the root does both.
A four-module build
model holds a record, pricing uses model, report uses model, and app uses pricing and report. The modules are listed in the root in a deliberately unhelpful order:
mkdir -p shop && cd shop
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shop</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>app</module>
<module>report</module>
<module>pricing</module>
<module>model</module>
</modules>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency><groupId>com.shop</groupId><artifactId>model</artifactId><version>${project.version}</version></dependency>
<dependency><groupId>com.shop</groupId><artifactId>pricing</artifactId><version>${project.version}</version></dependency>
<dependency><groupId>com.shop</groupId><artifactId>report</artifactId><version>${project.version}</version></dependency>
</dependencies>
</dependencyManagement>
</project>
POM
module() { # module <name> <dependency artifactIds...>
local name=$1; shift
local deps=""
for d in "$@"; do deps="$deps<dependency><groupId>com.shop</groupId><artifactId>$d</artifactId></dependency>"; done
mkdir -p $name/src/main/java/shop/$name
cat > $name/pom.xml <<POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent><groupId>com.shop</groupId><artifactId>shop-parent</artifactId><version>1.0.0</version></parent>
<artifactId>$name</artifactId>
<dependencies>$deps</dependencies>
</project>
POM
}
module model
module pricing model
module report model
module app pricing report
cat > model/src/main/java/shop/model/Item.java <<'SRC'
package shop.model;
public record Item(String name, int cents) {}
SRC
cat > pricing/src/main/java/shop/pricing/Pricing.java <<'SRC'
package shop.pricing;
import shop.model.Item;
public class Pricing {
public static int total(java.util.List<Item> items) { return items.stream().mapToInt(Item::cents).sum(); }
}
SRC
cat > report/src/main/java/shop/report/Report.java <<'SRC'
package shop.report;
import shop.model.Item;
public class Report {
public static String line(Item i) { return String.format("%-8s %6.2f", i.name(), i.cents() / 100.0); }
}
SRC
cat > app/src/main/java/shop/app/Main.java <<'SRC'
package shop.app;
import java.util.List;
import shop.model.Item;
import shop.pricing.Pricing;
import shop.report.Report;
public class Main {
public static void main(String[] args) {
List<Item> cart = List.of(new Item("coffee", 450), new Item("bagel", 275));
cart.forEach(i -> System.out.println(Report.line(i)));
System.out.println(Report.line(new Item("TOTAL", Pricing.total(cart))));
}
}
SRC
mvn package | grep -E '^\[INFO\] (Reactor Build Order|\S+ +\[(pom|jar)\]$)'
mvn package | sed -n '/Reactor Summary/,/BUILD/p' | sed 's/ \[ *[0-9.]* s\]//'
java -cp app/target/classes:model/target/classes:pricing/target/classes:report/target/classes shop.app.Main[INFO] Reactor Build Order:
[INFO] shop-parent [pom]
[INFO] model [jar]
[INFO] pricing [jar]
[INFO] report [jar]
[INFO] app [jar]
[INFO] Reactor Summary for shop-parent 1.0.0:
[INFO]
[INFO] shop-parent ........................................ SUCCESS
[INFO] model .............................................. SUCCESS
[INFO] pricing ............................................ SUCCESS
[INFO] report ............................................. SUCCESS
[INFO] app ................................................ SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
coffee 4.50
bagel 2.75
TOTAL 7.25
The reactor ignored the <modules> order and sorted by dependencies: model first, app last. Within one build, app compiles against pricing/target/classes, not against a pricing JAR in ~/.m2. Nothing needs to be installed for a multi-module build to work, which is why mvn install is rarely the right command (more in install & deploy).
The ${project.version} entries in the parent's dependencyManagement mean modules depend on each other without repeating the version; bump the parent's version and everything moves together.
Building part of the reactor: -pl, -am, -amd
In a big build you often want a few modules, not all of them:
| Option | Meaning |
|---|---|
-pl app (--projects) |
build only these modules (comma-separated; :artifactId or folder) |
-am (--also-make) |
plus everything they depend on |
-amd (--also-make-dependents) |
plus everything that depends on them |
-rf :report (--resume-from) |
restart a failed build from this module |
mkdir -p shop && cd shop
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shop</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules><module>model</module><module>pricing</module><module>report</module><module>app</module></modules>
<properties><maven.compiler.release>21</maven.compiler.release></properties>
</project>
POM
module() {
local name=$1; shift
local deps=""
for d in "$@"; do deps="$deps<dependency><groupId>com.shop</groupId><artifactId>$d</artifactId><version>1.0.0</version></dependency>"; done
mkdir -p $name
cat > $name/pom.xml <<POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent><groupId>com.shop</groupId><artifactId>shop-parent</artifactId><version>1.0.0</version></parent>
<artifactId>$name</artifactId>
<dependencies>$deps</dependencies>
</project>
POM
}
module model
module pricing model
module report model
module app pricing report
order() { mvn "$@" validate | grep -E '^\[INFO\] \S+ +\[(pom|jar)\]