skip to content

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