BOM(Bill of Materials)是由Maven提供的功能,它經過定義一整套相互兼容的jar包版本集合,使用時只須要依賴該BOM文件,便可放心的使用須要的依賴jar包,且無需再指定版本號。BOM的維護方負責版本升級,並保證BOM中定義的jar包版本之間的兼容性。java
使用BOM除了能夠方便使用者在聲明依賴的客戶端時不須要指定版本號外,最主要的緣由是能夠解決依賴衝突,如考慮如下的依賴場景:spring
項目A依賴項目B 2.1和項目C 1.2版本: 項目B 2.1依賴項目D 1.1版本; 項目C 1.2依賴項目D 1.3版本;
在該例中,項目A對於項目D的依賴就會出現衝突,按照maven dependency mediation的規則,最後生效的多是:項目A中會依賴到項目D1.1版本(就近原則,取決於路徑和依賴的前後,和Maven版本有關係)。
在這種狀況下,因爲項目C依賴1.3版本的項目D,可是在運行時生效的確是1.1版本,因此在運行時很容易產生問題,如 NoSuchMethodError, ClassNotFoundException等。apache
BOM本質上是一個普通的POM文件,區別是對於使用方而言,生效的只有<dependencyManagement>
這一個部分。只須要在<dependencyManagement>
定義對外發布的客戶端版本便可:json
<?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> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.1</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> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <!-- 經常使用工具 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.14.4</version> </dependency> </dependencies> </dependencyManagement> <build> </build> <distributionManagement> <repository> <id>maven-releases</id> <name>maven-releases</name> <url>http://mvn.ydj.com/repository/maven-releases/</url> </repository> <snapshotRepository> <id>maven-snapshots</id> <name>maven-snapshots</name> <url>http://mvn.ydj.com/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> </project>
<dependencyManagement></dependencyManagement>
節點下首位處加入以下:<dependencyManagement> <dependencies> <dependency> <groupId>com.jlcx.qd</groupId> <artifactId>inf-bom</artifactId> <version>${version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> ... </dependency> </dependencies> </dependencyManagement>
<dependencies></dependencies>
節點下引入以下:<dependencies> ... <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> .... </dependencies>
若是須要使用不一樣於當前bom中所維護的jar包版本,則加上<version>
覆蓋便可,如:segmentfault
<dependencies> ... <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> .... </dependencies>
歡迎掃碼關注微信公衆號或 我的博客