skip to content

Inheritance & Aggregation

How a child POM inherits from a parent, and why that is a different mechanism from an aggregator listing modules. Interviewers ask this to see whether you can explain a multi-module layout without conflating the two.

on this pageshow

questions

5

What is POM inheritance in Maven, and how does a child POM declare its parent?

level: juniorimportance: must knowfreq 70%

answer

  1. <parent> = groupId/artifactId/version
  2. Super POM = Central + layout + bindings
  3. effective-pom shows merge
  4. groupId/version inherited if omitted
  5. child can override

basics

~10 s

A child POM points at a parent POM via the <parent> element (groupId, artifactId, version). The child then automatically gets the parent's configuration unless it overrides it.

solid answer

~30 s

POM inheritance lets a child project reuse configuration from a parent POM declared in the child's <parent> block (groupId, artifactId, version, and optionally <relativePath>). Inherited things include <properties>, <dependencies>, <dependencyManagement>, <pluginManagement>, <build> config, <repositories>, and more. The child can override any of these. Crucially, EVERY POM also implicitly inherits from the built-in Super POM, which supplies defaults like Maven Central, the standard directory layout (src/main/java), and default plugin bindings. groupId and version themselves are inherited if omitted in the child. Inheritance is about merging configuration top-down; it is distinct from aggregation (a parent listing <modules> to build them together).

code

xml · 9 lines
xml
<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.acme</groupId>
    <artifactId>acme-parent</artifactId>
    <version>1.0.0</version>
  </parent>
  <artifactId>acme-service</artifactId>
</project>

go deeper

for a junior

Know the <parent> block needs groupId/artifactId/version and that children reuse parent config.

for a middle

Know the Super POM supplies Central + layout + default bindings, and which elements inherit.

for a senior

Reason about the effective-POM merge order and when to override vs inherit.

for a principal

Define org-wide parent POM strategy and what belongs at the root vs leaf modules.

## What inheritance means Maven projects are described by a `pom.xml` (Project Object Model). **Inheritance** lets one POM (the *child*) take configuration from another POM (the *parent*) so you don't repeat the same versions, plugin setup, and properties in every module. The child opts in with a `<parent>` element naming the parent's `groupId`, `artifactId`, and `version`. After that, Maven builds an *effective POM* by merging parent values into the child. ## The Super POM (the invisible root) Every POM implicitly inherits from the **Super POM**, which ships inside Maven itself. It provides defaults you never have to write: - The **Maven Central** repository (`https://repo.maven.apache.org/maven2`). - The **standard directory layout**: `src/main/java`, `src/test/java`, `target/`, etc. - Default **plugin bindings** for the lifecycle (compiler, surefire, jar, install...). You can see the fully merged result with `mvn help:effective-pom`. ## What gets inherited - `groupId` and `version` (if the child omits them) - `<properties>` - `<dependencies>` (actual dependencies — added to the child) - `<dependencyManagement>` (version/scope recommendations, not added) - `<build>` including `<pluginManagement>` and `<plugins>` - `<repositories>`, `<pluginRepositories>`, `<distributionManagement>` - `<developers>`, `<scm>`, `<properties>`, reporting config NOT inherited as-is: `artifactId` (must be unique), and `<modules>` (aggregation, not inheritance). ## Minimal example ```xml <project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.acme</groupId> <artifactId>acme-parent</artifactId> <version>1.0.0</version> </parent> <!-- groupId and version inherited from parent --> <artifactId>acme-service</artifactId> </project> ``` Here the child only declares its own `artifactId`; `groupId` becomes `com.acme` and `version` becomes `1.0.0` from the parent.

  • How do you see the final merged POM?
    Run `mvn help:effective-pom`; it prints the POM after merging the Super POM, all parents, and active profiles.
  • Does the child inherit the parent's artifactId?
    No. artifactId must be unique per project, so it is one of the few elements not inherited.

Like a class extending a base class: the subclass gets the base's fields/methods for free and can override them.

saying these in an interview costs you the question

  • Saying every project lists Maven Central itself (it comes from the Super POM)
  • Confusing inheritance (<parent>) with aggregation (<modules>)
  • Claiming you must always repeat groupId/version in children

context

open as a page

What is the difference between inheriting <dependencyManagement> and inheriting <dependencies> from a parent POM?

level: seniorimportance: must knowfreq 65%

basics

~10 s

<dependencies> in the parent are actually added to every child. <dependencyManagement> only sets default versions/scopes; a child still has to declare the dependency (without a version) to use it.

open as a page

Explain the difference between inheritance and aggregation (the <modules> reactor) in Maven multi-module builds.

level: seniorimportance: must knowfreq 60%

basics

~10 s

Inheritance (<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.

open as a page

How are <properties> and plugin configuration merged across the inheritance chain, and how does a child override them?

level: middleimportance: should knowfreq 45%

basics

~10 s

Properties and config are merged from the Super POM down through parents to the child. Anything the child redefines wins. So the child's value overrides the parent's, which overrides the Super POM's.

open as a page

What does <relativePath> do in a <parent> declaration, and how does Maven resolve the parent POM?

level: middleimportance: should knowfreq 50%

basics

~10 s

<relativePath> tells Maven where on disk to find the parent POM. By default it is ../pom.xml. If the parent isn't there, Maven downloads it from a repository instead.

open as a page