作为Java 开发我们经常会遇到
java.lang.NoSuchMethodError
java.lang.ClassNotFoundException
自己定义的一堆依赖会出现版本冲突,导致有些方法、类版本不对。
通常公司会有一个团队专门维护一些通用的依赖版本,将这些版本信息固定到一个pom文件里,后期bom的维护方负责版本升级,确保各个依赖之间没有版本冲突。
本质上bom还是一个普通的pom文件,BOM是(Bill of Materials Dependency)依赖列表。
一个bom例子如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ydj.qd</groupId>
<artifactId>inf-bom</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>inf-bom</name>
<description>第三方jar包统一管理</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.version>4.3.15.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 阿里 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun.mns/aliyun-sdk-mns -->
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
<!-- Apache -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
业务方使用姿势
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.ydj.qd</groupId>
<artifactId>inf-bom</artifactId>
<version>1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
用户只需要在dependencyManagement声明对bom的依赖,然后就可以在实际使用的地方去掉version字段就可以了, 如果需要使用不同于bom中的版本直接使用<version>覆盖即可