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

BOM 依赖管理

BOM(Bill of Materials)统一管理一组依赖的版本,简化多依赖版本配置。

BOM 概念

定义

BOM 是特殊的 POM 文件,通过 dependencyManagement 定义一组依赖版本。

作用

作用说明
统一版本一组依赖统一版本号
简化配置无需逐个声明版本
保证兼容版本组合经过测试

导入 BOM

import scope

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>

关键元素

元素说明
type=pomBOM 类型为 POM
scope=import导入方式

Spring Boot BOM

导入

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>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
</dependencies>

Spring Cloud BOM

导入

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

使用

XML
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  <!-- 版本由 BOM 管理 -->
</dependency>

多 BOM 导入

导入顺序

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

    <!-- Spring Cloud BOM -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>2021.0.3</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

优先级

后导入的 BOM 覆盖先导入的相同依赖版本。

自定义 BOM

创建 BOM POM

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

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.example</groupId>
        <artifactId>module-a</artifactId>
        <version>1.0.0</version>
      </dependency>
      <dependency>
        <groupId>com.example</groupId>
        <artifactId>module-b</artifactId>
        <version>1.0.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

使用自定义 BOM

XML
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>my-bom</artifactId>
      <version>1.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>module-a</artifactId>
    <!-- 版本由 BOM 管理 -->
  </dependency>
</dependencies>

BOM 版本覆盖

在使用方覆盖

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

    <!-- 覆盖特定依赖版本 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.32</version>
    </dependency>
  </dependencies>
</dependencyManagement>

查看 BOM 内容

使用 help 插件

Bash
mvn dependency:tree -Dverbose

查看 BOM 依赖

https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/2.7.0/spring-boot-dependencies-2.7.0.pom

常用 BOM 列表

BOM说明
spring-boot-dependenciesSpring Boot 依赖集
spring-cloud-dependenciesSpring Cloud 依赖集
jackson-bomJackson JSON 库
junit-bomJUnit 5 依赖集

BOM vs 直接声明

方式优点
BOM版本统一、保证兼容
直接声明灵活控制单个版本

要点总结

  • BOM 统一管理一组依赖版本
  • type=pom,scope=import 导入 BOM
  • 使用 BOM 后依赖无需声明版本
  • 多 BOM 导入,后导入覆盖先导入
  • Spring Boot、Spring Cloud 提供官方 BOM
  • 可创建自定义 BOM 管理内部依赖

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

← 上一篇 父 POM 继承与依赖管理
下一篇 → 依赖分类器使用
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

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

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