Maven BOM!拿来吧你

「本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2万元奖池等你挑战!前端

what BOM?

BOM(Bill of Materials)是由Maven提供的功能,它经过定义一整套相互兼容的jar包版本集合,java

使用时只须要依赖该BOM文件,便可放心的使用须要的依赖jar包,且无需再指定版本号web

BOM的维护方负责版本升级,并保证BOM中定义的jar包版本之间的兼容性。spring

why BOM?

使用BOM除了能够方便使用者在声明依赖的客户端时不须要指定版本号外,后端

最主要的缘由是能够解决依赖冲突,如考虑如下的依赖场景:markdown

项目A依赖项目B 2.1和项目C 1.2版本:框架

项目B 2.1依赖项目D 1.1版本;maven

项目C 1.2依赖项目D 1.3版本;spring-boot

在该例中,项目A对于项目D的依赖就会出现冲突,按照maven dependency mediation的规则,最后生效的多是:项目A中会依赖到项目D1.1版本(就近原则,取决于路径和依赖的前后,和Maven版本有关系)。oop

在这种状况下,因为项目C依赖1.3版本的项目D,可是在运行时生效的确是1.1版本,

因此在运行时很容易产生问题,如 NoSuchMethodError, ClassNotFoundException等,

有些jar包冲突定位仍是比较难的,这种方式能够节省不少定位此类问题的时间。

Spring、SpringBoot、SpringCloud自身都采用了此机制来解决第三方包的冲突,

常见官方提供的BOM:

1) RESTEasy Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-bom</artifactId>
            <version>3.0.6.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码

2. JBOSS Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-tools</artifactId>
            <version>${some.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement> 
复制代码

3) Spring Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.0.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码

4) Jersey Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码

5) SpringCloud SpringBoot Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码

看着有点蒙没关系,下面会详细介绍这些配置的做用

本身开发的项目中也建议使用此优良传统, 尤为实在项目开发初期,在后期再修改为BOM可能涉及不少版本的修改,就比较难了。

how BOM?

定义BOM

BOM本质上是一个普通的POM文件,区别是对于使用方而言,生效的只有 <dependencyManagement>这一个部分。

只须要在<dependencyManagement>定义对外发布的客户端版本便可,

好比须要在项目中统一全部SpringBoot和SpringCloud的版本

第一步须要在POM文件中增长两个的官方BOM,以目前最新稳定的SpringBoot版本为例,使用官方推荐的版本组合比较稳定,通常不会有什么大的问题

image.png

<groupId>com.niu.not</groupId>
<artifactId>niu-dependency</artifactId>
<version>1.1.1</version>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
</dependencies>
复制代码

下面的Gson是除了SpringBoot和SpingCloud外须要统一版本的jar

其余工程使用方法

在项目主pom.xml文件中<dependencyManagement></dependencyManagement>节点下加入BOM的GAV信息以下:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.niu.not</groupId>
            <artifactId>niu-dependency</artifactId>
            <version>1.1.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码

在须要使用相关JAR包的pom.xml文件中<dependencies></dependencies>节点下引入以下:

<dependencies>
    <!--此时用到Spring和Gson都不须要加版本号,会自动引用BOM中提供的版本-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>
</dependencies>
复制代码

这种设置后,若是项目要求升级Spring版本,只须要在提供方升级验证兼容性,而后修改BOM依赖便可

若是须要使用不一样于当前bom中所维护的jar包版本,则加上<version>覆盖便可,如:

<dependencies>
    <!--此时用到Spring和Gson都不须要加版本号,会自动引用BOM中提供的版本-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <!--会覆盖掉BOM中声明的版本2.8.6,使用自定义版本2.8.2-->
        <version>2.8.2</version>
    </dependency>
</dependencies>
复制代码

小结

Jar包冲突很是烦人,Spring框架相关的冲突,有些报错很是不清晰或者根本不报错直接中止服务,

这种问题很难定位,开发人员应该聚焦业务开发,不该该在这上面浪费过多时间,

因此统一的版本管理仍是很是有用的,否则Spring的牛逼框架为啥都在用呢,

BOM管理,拿来吧你!!!

参考:howtodoinjava.com/maven/maven…

相关文章
相关标签/搜索