溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微服務(wù)中如何使用Maven的BOM來管理版本依賴

發(fā)布時(shí)間:2021-09-23 14:57:38 來源:億速云 閱讀:136 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“微服務(wù)中如何使用Maven的BOM來管理版本依賴”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微服務(wù)中如何使用Maven的BOM來管理版本依賴”這篇文章吧。

BOM簡介

BOM(Bill of Materials)是由Maven提供的功能,它通過定義一整套相互兼容的jar包版本集合,使用時(shí)只需要依賴該BOM文件,即可放心的使用需要的依賴jar包,且無需再指定版本號(hào)。BOM的維護(hù)方負(fù)責(zé)版本升級(jí),并保證BOM中定義的jar包版本之間的兼容性。

為什么要使用BOM

使用BOM除了可以方便使用者在聲明依賴的客戶端時(shí)不需要指定版本號(hào)外,最主要的原因是可以解決依賴沖突,如考慮以下的依賴場(chǎng)景:

項(xiàng)目A依賴項(xiàng)目B 2.1和項(xiàng)目C 1.2版本: 項(xiàng)目B 2.1依賴項(xiàng)目D 1.1版本; 項(xiàng)目C 1.2依賴項(xiàng)目D 1.3版本;

在該例中,項(xiàng)目A對(duì)于項(xiàng)目D的依賴就會(huì)出現(xiàn)沖突,按照maven dependency mediation的規(guī)則,最后生效的可能是:項(xiàng)目A中會(huì)依賴到項(xiàng)目D1.1版本(就近原則,取決于路徑和依賴的先后,和Maven版本有關(guān)系)。

在這種情況下,由于項(xiàng)目C依賴1.3版本的項(xiàng)目D,但是在運(yùn)行時(shí)生效的確是1.1版本,所以在運(yùn)行時(shí)很容易產(chǎn)生問題,如 NoSuchMethodError, ClassNotFoundException等。

如何定義BOM

BOM本質(zhì)上是一個(gè)普通的POM文件,區(qū)別是對(duì)于使用方而言,生效的只有<dependencyManagement>這一個(gè)部分。只需要在<dependencyManagement>定義對(duì)外發(fā)布的客戶端版本即可:

<?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>第三方j(luò)ar包統(tǒng)一管理</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>

項(xiàng)目使用方法

在你的項(xiàng)目主pom.xml文件中<dependencyManagement></dependencyManagement>節(jié)點(diǎn)下首位處加入如下:

<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>

在需要使用相關(guān)JAR包的pom.xml文件中<dependencies></dependencies>節(jié)點(diǎn)下引入如下:

<dependencies>  ...  <dependency>    <groupId>com.google.guava</groupId>    <artifactId>guava</artifactId>  </dependency>  <dependency>    <groupId>commons-collections</groupId>    <artifactId>commons-collections</artifactId>  </dependency>  ....</dependencies>

如果需要使用不同于當(dāng)前bom中所維護(hù)的jar包版本,則加上<version>覆蓋即可,如:

<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>

以上是“微服務(wù)中如何使用Maven的BOM來管理版本依賴”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI