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

父 POM 继承与依赖管理

父 POM 通过 dependencyManagement 统一管理依赖版本,子模块无需声明版本。

dependencyManagement 作用

功能

  • 统一依赖版本
  • 不实际引入依赖
  • 子模块继承版本声明

对比 dependencies

元素作用
dependencies直接引入依赖
dependencyManagement仅管理版本

父 POM 配置

完整示例

XML
<project>
  <groupId>com.example</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <properties>
    <spring.version>5.3.20</spring.version>
    <junit.version>4.13.2</junit.version>
    <mysql.version>8.0.28</mysql.version>
  </properties>

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

      <!-- JUnit -->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
      </dependency>

      <!-- MySQL -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

子模块引用

无版本声明

XML
<project>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
  </parent>

  <artifactId>service</artifactId>

  <dependencies>
    <!-- 版本继承 dependencyManagement -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <!-- 无需声明 version -->
    </dependency>
  </dependencies>
</project>

继承 Spring Boot BOM

使用 spring-boot-dependencies

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

子模块使用

XML
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 版本由 BOM 管理 -->
  </dependency>
</dependencies>

pluginManagement

统一插件版本

XML
<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>17</source>
          <target>17</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

子模块使用

XML
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <!-- version 继承 -->
    </plugin>
  </plugins>
</build>

版本更新管理

更新流程

XML
1. 父 POM 更新版本属性
2. 所有子模块自动同步
3. 无需逐个修改子模块

示例

XML
<!-- 父 POM -->
<properties>
  <spring.version>5.3.21</spring.version>  <!-- 更新版本 -->
</properties>

<!-- 所有子模块自动使用新版本 -->

可继承元素

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

覆盖父 POM 版本

子模块指定版本

XML
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.2.0</version>  <!-- 覆盖父版本 -->
</dependency>

不推荐覆盖,会导致版本不一致。

relativePath 配置

本地父 POM

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

远程父 POM

text
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.7.0</version>
  <relativePath/>  <!-- 从仓库查找 -->
</parent>

要点总结

  • dependencyManagement 仅管理版本,不引入依赖
  • 子模块无需声明 version,继承父版本
  • 使用 properties 统一版本号
  • pluginManagement 统一插件版本
  • Spring Boot BOM 可 import 导入
  • 更新父版本,子模块自动同步
  • relativePath 空值从仓库查找父 POM

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

← 上一篇 模块间依赖关系管理
下一篇 → BOM 依赖管理
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

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

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