• dependencyManagement与dependencies区别


    一、dependencyManagement应用场景

    为了项目的正确运行,必须让所有的子模块使用依赖项的统一版本,必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的结果。在我们项目顶层的pom文件中,我们会看到dependencyManagement元素。通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。

    父pom中dependencyManagement如下:

       <modules>
            <module>module1</module>
        </modules>
        <properties>
                <spring-version>3.1.1.RELEASE</spring-version>
        </properties>
    
        <dependencyManagement>
              <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>${spring-version}</version>
              </dependency>
        </dependencyManagement>
    

    子模块module1中dependency声明如下所示:

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

    这样做的好处:统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,才能保证测试的和发布的是相同的成果,因此,在顶层pom中定义共同的依赖关系。同时可以避免在每个使用的子项目中都声明一个版本号,这样想升级或者切换到另一个版本时,只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个版本号时,只需要在dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号。

    二、dependencies应用场景

    相对于dependencyManagement,如果在父pom文件中中通过dependencies引入jar,将默认被所有的子模块继承。
    子模块如果希望有自己个性化的内容,可以在子模块中对于其中的某个属性进重新定义。
    例如:
    父pom中:

        <dependencyManagement>
           <dependencies>
              ....
              <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>3.1</version>
              </dependency>
              ....
          </dependencies>
        </dependencyManagement>
    

    子模块1的pod中:

    <dependencies>
              ....
              <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>3.2</version>
                  <exclusions>
                      <exclusion>
                            <groupId>com.alibaba</groupId>
                            <artifactId>fastjson</artifactId>
                        </exclusion>
                  </exclusions>
              </dependency>
              ....
          </dependencies>
    

    那么,子模块1由于重新定义了spring-web的描述,因此子模块将采用自己的依赖定义,而不再使用pom中默认定义的。

    三、dependencyManagement与dependencies区别

    dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

    dependencies即使在子模块中不写该依赖项,那么子模块仍然会从父项目中继承该依赖项(全部继承)。

    在实际的项目开发中,推荐在父pom中使用dependencyManagement对项目中使用到的依赖包进行统一的管理。



    作者:GeekerLou
    链接:https://www.jianshu.com/p/c8666474cf9a
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    Keepalived 无法自动转换主备角色,请关注 iptables 防火墙配置
    Linux 下使用网易的SMTP服务器 发送邮件
    Spring-boot 最小demo
    go build 时报错 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
    spark-shell 执行脚本并传入参数
    JVM
    spark
    spark
    linux
    linux
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/15189395.html
Copyright © 2020-2023  润新知