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=pom | BOM 类型为 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-dependencies | Spring Boot 依赖集 |
| spring-cloud-dependencies | Spring Cloud 依赖集 |
| jackson-bom | Jackson JSON 库 |
| junit-bom | JUnit 5 依赖集 |
BOM vs 直接声明
| 方式 | 优点 |
|---|---|
| BOM | 版本统一、保证兼容 |
| 直接声明 | 灵活控制单个版本 |
要点总结
- BOM 统一管理一组依赖版本
- type=pom,scope=import 导入 BOM
- 使用 BOM 后依赖无需声明版本
- 多 BOM 导入,后导入覆盖先导入
- Spring Boot、Spring Cloud 提供官方 BOM
- 可创建自定义 BOM 管理内部依赖
📝 发现内容有误?点击此处直接编辑