关于<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

相关推荐
七歌杜金房9 小时前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SelectDB13 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码21 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵1 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
tntxia1 天前
linux curl命令详解_curl详解
linux
Lyn_Li1 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
扛枪的书生1 天前
Linux 网络管理器用法速查
linux
小九九的爸爸2 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
顺风尿一寸2 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
阿耶同学2 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员