全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-23 6 分钟 ✍️ juanwangdev

继承与聚合

继承实现 POM 配置复用,聚合实现多模块统一构建。

继承(parent)

继承结构

XML
父 POM(配置模板)
    ↓
子模块 A(继承父配置)
子模块 B(继承父配置)

父 POM 示例

XML
<!-- parent-project/pom.xml -->
<project>
  <groupId>com.example</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>  <!-- 父模块必须为 pom -->

  <properties>
    <java.version>17</java.version>
    <spring.version>5.3.0</spring.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

子模块继承

XML
<!-- module-a/pom.xml -->
<project>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <relativePath>../parent-project/pom.xml</relativePath>
  </parent>

  <artifactId>module-a</artifactId>
  <!-- groupId、version 可继承 -->

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <!-- version 继承 dependencyManagement -->
    </dependency>
  </dependencies>
</project>

继承内容

可继承元素说明
groupId组织标识
version版本号
properties属性定义
dependencyManagement依赖版本管理
pluginManagement插件版本管理
repositories仓库配置
build 配置构建配置

聚合(modules)

聚合结构

Bash
聚合 POM(管理多个模块)
    ├── module-a
    ├── module-b
    └── module-c

聚合 POM 示例

XML
<project>
  <groupId>com.example</groupId>
  <artifactId>aggregator-project</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>  <!-- 聚合模块必须为 pom -->

  <modules>
    <module>module-a</module>
    <module>module-b</module>
    <module>module-c</module>
  </modules>
</project>

聚合构建

XML
# 在聚合目录执行,构建所有模块
mvn install

# 构建顺序自动计算依赖关系
[INFO] Reactor Build Order:
[INFO] module-a
[INFO] module-b
[INFO] module-c

继承+聚合组合

父模块同时作为聚合

text
<!-- 父 POM 同时聚合子模块 -->
<project>
  <groupId>com.example</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <!-- 聚合子模块 -->
  <modules>
    <module>module-a</module>
    <module>module-b</module>
  </modules>

  <!-- 继承配置:properties、dependencyManagement -->
  <properties>
    <java.version>17</java.version>
  </properties>
  ...
</project>

目录结构

text
parent-project/
├── pom.xml              <!-- 父+聚合 -->
├── module-a/
│   └── pom.xml          <!-- 继承 parent -->
├── module-b/
│   └── pom.xml          <!-- 继承 parent -->

继承与聚合区别

特性继承聚合
目的配置复用统一构建
packaging父模块为 pom聚合模块为 pom
配置关系子继承父无配置继承
构建行为子模块独立构建聚合触发全部构建
结合使用常与聚合配合常与继承配合

relativePath 配置

text
<parent>
  <groupId>com.example</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
  <!-- 默认值为 ../pom.xml -->
  <relativePath>../parent/pom.xml</relativePath>
</parent>

空值 <relativePath/> 表示不从本地查找,直接从仓库查找父 POM。

要点总结

  • 继承:子模块继承父 POM 配置,实现配置复用
  • 聚合:modules 定义子模块,实现统一构建
  • 父模块 packaging 必须为 pom
  • dependencyManagement 只管理版本,不引入依赖
  • 继承和聚合常组合使用,父模块同时聚合子模块

📝 发现内容有误?点击此处直接编辑

← 上一篇 构建配置基础
下一篇 → 项目信息配置
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库