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

相关推荐
程序媛一枚~2 小时前
✨✨✨使用Python,OpenCV及图片拼接生成❤️LOVE❤️字样图,每张小图加随机颜色边框,大图加随机大小随机颜色边框
图像处理·python·opencv·numpy·图像拼接
jyfool2 小时前
Ubuntu 远程桌面配置踩坑实录:从 TightVNC 到 x11vnc 的折腾之旅
linux·运维·ubuntu
MediaTea2 小时前
Python:collections.Counter 常用函数及应用
开发语言·python
如若1232 小时前
flash-attn 安装失败?从报错到成功的完整排雷指南(CUDA 12.8 + PyTorch 2.7)
人工智能·pytorch·python
007张三丰2 小时前
知乎高赞回答爬虫:从零开始,建立你的专属知识库
爬虫·python·知识库·python爬虫·知乎·高赞回答
安当加密3 小时前
基于 RADIUS 的 Linux 服务器双因子认证:从 FreeRADIUS 到轻量级 ASP 方案的演进
linux·运维·服务器
LawrenceLan3 小时前
37.Flutter 零基础入门(三十七):SnackBar 与提示信息 —— 页面反馈与用户交互必学
开发语言·前端·flutter·dart
李昊哲小课3 小时前
Python json模块完整教程
开发语言·python·json