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

     

  • 相关阅读:
    angularjs中的页面访问权限设置
    Html页面head标签元素的意义和应用场景
    脚本引用中的defer和async的用法和区别
    自适应页面中如何使用雪碧图
    网页颜色分辨测试小游戏的js化辨别及优化
    jQuery1.9及其以上版本中动态元素on绑定事件无效解决方案
    Data URL简介及Data URL的利弊
    浏览器调试:事件定位与源码查找
    简述ES5 ES6
    移动端中pagehide、pageshow的应用
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/9795046.html
Copyright © 2011-2022 走看看