Spring Boot 依赖管理-BOM说明

2,789 阅读2分钟

Spring Boot 依赖管理-BOM

什么是BOM

Maven Packaging

maven 项目打包类型常见的分为3种:

  • jar 打包为普通的java项目,格式为jar类型。
  • pom 可以作为多模块项目的管理维护,如在父模块中定义依赖、插件等进行统一的版本维护管理。
  • war war 可运行的项目,打包为java web项目,运行在诸如:Tomcat、jetty等应用服务器上。

BOM(Bill Of Materials):物料清单,项目结构文件,定义了我们需要的统一的依赖版本管理清单。BOM 实际上也是一个类型为POM的普通Maven项目,只是该项目主要维护描述了Maven项目需要一系列公共的依赖信息。通过引用该项目,从而可以统一的进行依赖的维护管理(不需要明确的指明版本号)

BOM定义管理

BOM 文件结构定义结构

<groupId>com.caitxj</groupId>
<artifactId>caitxj-boot-dependencies</artifactId>
<version>1.0-SNAPSHOT</version>
<!--pom类型,多模块中依赖统一管理维护-->
<packaging>pom</packaging>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencyManagement>
    <dependencies>
        <!--统一维护管理的依赖-->
    </dependencies>
</dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            <!--插件依赖统一管理-->
        </plugins>
    </pluginManagement>
</build>

BOM清单使用


<dependencies>
   <!--引入Spring Boot官方维护的bom依赖清单-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.6.3</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <!--引入阿里依赖bom清单-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>aliyun-spring-boot-dependencies</artifactId>
      <version>1.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
</dependencies>

说明: dependency中的type(pom:代表引入项目类型为pom的项目,即依赖管理项目),scope(import只有当类型为pom的时候使用,表示依赖清单的引入) 更多的参考Maven标签详解。

Spring Boot BOM 的设计及维护

常见BOM清单

Spring Boot
Spring Alibaba Boot

自定义BOM清单开发流程

  1. 定义一个Maven项目,类型为pom
  2. 定义dependencyManagement,并在properties中维护好已经测试过的依赖版本
  3. 引入第三方的权威、通用的一些BOM清单(如:Spring Boot,Alibaba的等)
  4. 打包上传服务器

自定义BOM清单设计思路

  • 基于功能块进行分类
    如: data-boot-dependencies:表示数据操作相关依赖清单(可以包含数据库操作相关、文件操作相关、excel、word、PDF等操作)