1. 程式人生 > 程式設計 >微服務中使用Maven BOM來管理你的版本依賴詳解

微服務中使用Maven BOM來管理你的版本依賴詳解

BOM簡介

BOM(Bill of Materials)是由Maven提供的功能,它通過定義一整套相互相容的jar包版本集合,使用時只需要依賴該BOM檔案,即可放心的使用需要的依賴jar包,且無需再指定版本號。BOM的維護方負責版本升級,並保證BOM中定義的jar包版本之間的相容性。

為什麼要使用BOM

使用BOM除了可以方便使用者在宣告依賴的客戶端時不需要指定版本號外,最主要的原因是可以解決依賴衝突,如考慮以下的依賴場景:

專案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等。

如何定義BOM

BOM本質上是一個普通的POM檔案,區別是對於使用方而言,生效的只有<dependencyManagement>這一個部分。只需要在<dependencyManagement>定義對外發布的客戶端版本即可:

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

專案使用方法

在你的專案主pom.xml檔案中<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>

在需要使用相關JAR包的pom.xml檔案中<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>覆蓋即可,如:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。