TExcelTAD
Home › Java Builds: Maven & Gradle › Multi-Module Reactor Builds

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:

bash · try it yourself
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
bash · try it yourself
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)\]
  

 | awk '{print $2}' | paste -sd' '; }
echo "-pl app compile:"; mvn -pl app compile | grep 'ERROR\] Failed' | cut -c1-160
echo "-pl app -am:      $(order -pl app -am)"
echo "-pl model -amd:   $(order -pl model -amd)"
echo "-pl report -amd:  $(order -pl report -amd)"
echo "-rf :report:      $(order -rf :report)"
-pl app compile:
[ERROR] Failed to execute goal on project app: Could not collect dependencies for project com.shop:app:jar:1.0.0
[ERROR] Failed to read artifact descriptor for com.shop:pricing:jar:1.0.0
[ERROR] Failed to read artifact descriptor for com.shop:report:jar:1.0.0
-pl app -am:      shop-parent model pricing report app
-pl model -amd:   model pricing report app
-pl report -amd:  report app
-rf :report:      report app

-pl app alone builds just app and resolves pricing and report from the local repository. Here nobody installed them, so it fails; on a developer machine where someone ran mvn install last week, it silently compiles against last week's code, which is worse. -pl app -am is the safe form: it builds exactly what app needs. -pl model -amd answers "what could my change to model break?", which is the basis of incremental CI for monorepos.

Tip: mvn -T 1C verify builds independent modules in parallel (one thread per CPU core). The reactor's dependency order still holds: pricing and report can build at the same time, app waits for both. Logs interleave, so keep -T for speed, not for reading output.

Test yourself

In what order does the reactor build modules?

  1. The order of <modules>
  2. Alphabetically
  3. A topological order of the dependencies between modules
  4. Random

You changed model. Which command builds model and every module that depends on it?

  1. mvn -pl model
  2. mvn -pl model -am
  3. mvn -pl model -amd
  4. mvn -rf model

What is the difference between aggregation and inheritance in Maven?

  1. They are the same
  2. Aggregation (<modules>) decides what a build includes; inheritance (<parent>) decides what configuration a module gets
  3. Inheritance builds modules in parallel
  4. Aggregation is only for BOMs