关于<dependencyManagement>和<dependencies>

dependencyManagement

作用:项目存在多个子模块,模块间引入相同的依赖项,为了项目的正确运行,避免依赖项版本冲突,管理依赖项,保证各模块依赖项版本号一致

项目顶层的 父pom文件中dependencyManagement元素。通过它来管理jar包的版本,让子模块中引用一个依赖而不用显示的列出版本号。子模块不指定依赖版本号时,maven会沿着子模块向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。

父pom:

XML 复制代码
<dependencyManagement>
   <dependencies>
      <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
      </dependency>
   <dependencies>
</dependencyManagement>

子pom:

XML 复制代码
<dependencies>
   <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
   </dependency>
</dependencies>

dependencies

相对于dependencyManagement,如果在父pom文件中通过dependencies引入jar,将默认被所有的子模块继承。

子模块如果希望有自己个性化的内容,可以在子模块中对于其中的某个属性进重新定义。

父 pom:

XML 复制代码
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
         <version>3.1</version>
      </dependency>
  </dependencies>
</dependencyManagement>

子pom:

(<exclusions><exclusion>作用:排除关联依赖的引入)

XML 复制代码
<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>

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

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

参考链接:https://www.jianshu.com/p/f8bd3c96f5ea

相关推荐
小张成长计划..3 分钟前
【Linux】18:基础IO
linux·运维·服务器
MIXLLRED37 分钟前
解决——Ubuntu远程桌面指令更换网络
linux·网络·ubuntu
啊哈一半醒1 小时前
Windows 虚拟机搭建 CentOS 全过程(2026 最新版 + 踩坑解决)
linux·windows·centos
wunaiqiezixin1 小时前
MIT 6.S081 lazy 前置篇(lab5):缺页异常、惰性分配、写时复制、页面换出
linux·unix·os·xv6·mit6.s081
Benszen1 小时前
云计算基础-13:Linux网络管理实战-1
linux·运维·云计算
二十雨辰2 小时前
[Java]-Spring面试题
java·开发语言
北斗落凡尘2 小时前
如何使用LangGraph(1)
python·langchain
萧瑟余晖2 小时前
Java深入解析篇二十二之虚拟线程
java·开发语言
_kerneler2 小时前
Linux tiny RCU
linux
油丶酸萝卜别吃3 小时前
Java 集合类全景介绍
java·开发语言