Explain the difference between inheritance and aggregation (the <modules> reactor) in Maven multi-module builds.
answer
- inheritance = config down via <parent>
- aggregation = <modules> reactor builds all
- packaging=pom for aggregator
- orthogonal but usually same root POM
- reactor topo-sorts build order
basics
~10 sInheritance (<parent>) shares configuration with children. Aggregation (<modules> in a pom-packaging project) groups submodules so one mvn command builds them all in dependency order. They are independent and often combined.
solid answer
~40 sInheritance and aggregation solve different problems and can be used separately or together. **Inheritance** flows configuration *downward*: a child names a `<parent>` and merges its properties, dependencyManagement, pluginManagement, etc. **Aggregation** is a *reactor* concern: a POM with `<packaging>pom</packaging>` lists `<modules>`, and running `mvn` from it builds every listed module in the correct order based on inter-module dependencies. The aggregator does not have to be the parent, and a parent does not have to aggregate — they're orthogonal. In practice many projects merge both: a single root POM acts as the parent (config) and the aggregator (<modules>). The reactor also computes the build order topologically and supports flags like `-pl`, `-am`, and `--fail-at-end`.
code
xml · 12 lines<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acme</groupId>
<artifactId>acme-aggregator</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>service</module>
<module>web</module>
</modules>
</project>go deeper
Know <parent> shares config and <modules> builds several projects at once.
Explain packaging=pom aggregator and that the reactor orders by dependencies.
Use reactor flags (-pl/-am/-rf) and explain why the two concepts are orthogonal.
Architect large multi-module repos: parent/BOM/aggregator split, build-order and CI partial-build strategy.
## Two orthogonal mechanisms Newcomers conflate these because a single root `pom.xml` usually does both. They are independent. ### Inheritance — sharing configuration Direction: **child -> parent**. The child declares `<parent>`; Maven merges the parent's config into the child. This is about *not repeating* versions, properties, and plugin setup. ### Aggregation (the reactor) — building together Direction: **aggregator -> modules**. A POM with `<packaging>pom</packaging>` lists child directories in `<modules>`: ```xml <project> <modelVersion>4.0.0</modelVersion> <groupId>com.acme</groupId> <artifactId>acme-aggregator</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>core</module> <module>service</module> <module>web</module> </modules> </project> ``` Running `mvn install` here is the **reactor**: Maven reads all listed modules, builds a dependency graph among them, sorts them **topologically**, and builds in that order. If `web` depends on `core`, `core` builds first regardless of `<modules>` order. ## They are separable - A module can have a `<parent>` that is **not** its aggregator. - An aggregator can list modules that do **not** inherit from it. Most teams nonetheless make one root POM both the parent and the aggregator for simplicity. ## Useful reactor flags - `-pl core,service` — build only these projects (project list). - `-am` — also make upstream dependencies of selected projects. - `-amd` — also make downstream dependents. - `--fail-at-end` (`-fae`) — keep building independent modules after one fails. - `-rf :service` — resume the reactor from a given module. ## Why the distinction matters in interviews If asked 'why does my submodule build even though I only ran the parent?', the answer is the **reactor (aggregation)**, not inheritance. If asked 'why does my submodule get the parent's plugin config?', that's **inheritance**.
- Does the order of <modules> determine build order?No. The reactor topologically sorts modules by their inter-dependencies, so a dependency builds before its dependents regardless of listing order.
- Can an aggregator and a parent be different POMs?Yes. Aggregation (<modules>) and inheritance (<parent>) are independent; a common but optional convention is to merge them into one root POM.
- What does mvn -pl service -am do?Builds only the 'service' project plus its upstream dependencies (also make), skipping unrelated modules in the reactor.
Inheritance is a family handing down house rules; aggregation is a tour bus that drives everyone to the same destinations in the right order. One is about shared rules, the other about a coordinated trip.
saying these in an interview costs you the question
- Saying <modules> order is the build order
- Claiming a submodule builds with the parent only because of inheritance
- Treating inheritance and aggregation as the same feature