Parent


https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
https://maven.apache.org/ref/current/maven-model/maven.html

1. Important Points#

parent POM:
    被当前项目继承的 POM,用来统一 properties、dependencyManagement、pluginManagement

effective POM:
    super POM + parent POM + current POM + active profiles 合并后的结果
./mvnw help:effective-pom
./mvnw help:active-profiles

2. Parent Declaration#

<parent>
  <groupId>com.example.platform</groupId>
  <artifactId>platform-parent</artifactId>
  <version>1.2.0</version>
  <relativePath>../pom.xml</relativePath>
</parent>
relativePath:
    先从本地路径找 parent
    不需要本地 parent 时可写空值
<relativePath/>

3. What Is Inherited#

Area Inherited Notes
properties yes child can override
dependencyManagement yes controls default dependency versions
dependencies yes use carefully; can force every child to include dependencies
pluginManagement yes recommended place for plugin versions
plugins yes executions may run in child modules
repositories yes better keep repository policy centralized
profiles limited activation and inherited effects need effective POM check
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.platform</groupId>
  <artifactId>platform-parent</artifactId>
  <version>1.2.0</version>
  <packaging>pom</packaging>

  <properties>
    <java.version>21</java.version>
    <maven.compiler.release>${java.version}</maven.compiler.release>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>3.3.1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.13.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

5. Child Override#

<properties>
  <java.version>17</java.version>
</properties>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.17.2</version>
    </dependency>
  </dependencies>
</dependencyManagement>
override rule:
    child POM can override many parent defaults
    verify final result with effective-pom

6. Plugin Inheritance#

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.4.0</version>
  <inherited>false</inherited>
</plugin>
inherited=false:
    parent config does not propagate to child modules
    useful for aggregator-only plugins

7. Common Mistakes#

parent not found:
    check relativePath, repository, and version

plugin version unclear:
    inspect effective POM
    move version to pluginManagement

child override seems ignored:
    check property name
    check active profiles
    check dependencyManagement order