微服務之BOM定義

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

為什麼要使用BOM

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

如何定義BOM

	BOM 本質上是一個普通的POM檔案,區別是對於使用方而言,生效的只有 **  dependencyManament  ** 這一個部分,也只是需要在 **  dependencyManament  **  定義對於對外發布的客戶端版本即可:
<?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.test.xx</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>

專案中如何使用BOM

在你的專案主pom.xml檔案中 節點下首位處加入如下

<dependencyManagement>
    <dependencies>
         <dependency>
            <groupId>com.test.xx</groupId>
            <artifactId>cloud-bom</artifactId>
            <version>${version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        
          <dependency>
            ...
          </dependency>
    </dependencies>
</dependencyManagement>

在需要使用相關JAR包的pom.xml檔案中節點下引入如下:

<dependencies>
 ...
	 <dependency>
	  <groupId>com.google.guava</groupId>
	  <artifactId>guava</artifactId>
	 </dependency>

	 <dependency>
	  <groupId>commons-collections</groupId>
	  <artifactId>commons-collections</artifactId>
	 </dependency>
 ....
</dependencies>

相關文章