maven之BOM及BOM和provided的一個小坑


  BOM(Bill of Materials)定義一整套相互兼容的jar包版本集合,使用時只需要依賴該BOM文件,即可放心的使用需要的依賴jar包,且無需再指定版本號。BOM的維護方負責版本升級,並保證BOM中定義的jar包版本之間的兼容性。

 

  

  子模塊很多時,可以使用dependencyManagement在父模塊中統一管理。

  父模塊中配置:

<groupId>maven</groupId>
<artifactId>X</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.2.3.RELEASE</version>
            </dependency>
        </dependencies>
</dependencyManagement>

  packaging不一定是pom,也可以是jar和war。

  子模塊則無需指定版本信息:

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>maven</groupId>
        <artifactId>X</artifactId>
        <version>1.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
 </dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

 

  有2點擴展:

       1. 子模塊可以繼承多個父模塊

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>maven</groupId>
        <artifactId>X</artifactId>
        <version>1.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>maven</groupId>
        <artifactId>Y</artifactId>
        <version>1.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  2.父模塊定義的是provided時,子模塊在引用時要小心。

          這種應用場景是,多個微服務的docker鏡像依賴於一個基礎鏡像,則可以將基礎鏡像中集成的公共jar包做成BOM,則各微服務依賴的jar包可以做到統一。

     provided是沒有傳遞性的,也就是說,如果你依賴的某個jar包,它的某個jar的范圍是provided,那么該jar不會在你的工程中依靠jar依賴傳遞加入到你的工程中。

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.2.3.RELEASE</version>
                 <scope>provided</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

    子工程引用該pom時,發現classpath中沒有從父類中集成到provided范圍的jar包。

            如果使用intellj,版本在2018之后,可以使用以下方法把provided范圍的jar包加到classpath中。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM