zoukankan      html  css  js  c++  java
  • 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中。

     

  • 相关阅读:
    数组
    字符对象的方法
    事件
    判断数据类型
    数据类型和变量
    语法
    快速入门
    JavaScript简介
    Spring init-method和destroy-method属性的使用
    spring3后提供了的context:property-placeholder/元素
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/9795046.html
Copyright © 2011-2022 走看看