Spring Cloud项目一般会搭建为Mavne多模块项目,常见到dependencyManagement,存在两种情况

一、在父项目中的dependencyManagement,它是对所依赖jar包进行声明依赖,继承该父项目的子项目,不会直接引入dependencyManagement管理的jar包。因此子项目需要显式的声明需要用的依赖,并且没有指定version,才会从父项目中继承该依赖,这时version和scope都读取自父pom;

如果子项目中指定了版本号,那么会使用子项目中指定的jar版本.;

如果不在子项目中声明依赖,是不会从父项目中继承下来的;

而不包含在dependencyManagement中的dependencies中的依赖,即使在子项目中不写该依赖项,仍然会从父项目中继承该依赖项(全部继承)

如下

<!-- 会实际下载jar包,子项目会继承这些依赖  -->
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies><!-- 只是对版本进行管理,不会实际引入jar,子项目继承时必须显示声明,才会引入该依赖  -->
<dependencyManagement><dependencies><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency></dependencies>
</dependencyManagement>

二、子项目自身使用dependencyManagement,
注意:此处 dependencyManagement 不是版本管理,而是导入多个父模块
这样的用法必须在当前pom中使用dependencyManagement 标签,并且添加上

<type>pom</type>和<scope>import</scope>标签

这是为了解决maven 单继承的问题,使用这种方式,子模块不仅可以继承parent标签中的模块,也可以继承dependencyManagement中的其它模块,如下

<!--当前模块同时继承spring-boot-starter-parent、spring-boot-dependencies  和 spring-cloud-dependencies-->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.4.RELEASE</version><relativePath />
</parent>
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.4.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

如果大家不知道spring-cloud的版本如何选择,可以参考这位哥们的
SpringBoot与SpringCloud的版本对应详细版

另外:maven依赖中的scope选项有
compile(默认选项):表示为当前依赖参与项目的编译、测试和运行阶段,scope的默认选项。

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

test :表示为当前依赖只参与测试阶段。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

runtime :表示为当前依赖只参与运行阶段,一般这种类库都是接口与实现相分离的类库,如mysql的驱动包。

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency>

privided :表示为当前依赖在打包过程中,不需要打进去,需要由运行的环境来提供,比如tomcat或者基础类库,如a依赖于b和c,bc中都包含gson依赖(privided),则a中不会依赖bc中的gson,需由a自行引入gson依赖。

<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><scope>provided</scope>
</dependency>

system :表示为当前依赖不从maven仓库获取,从本地系统获取,结合systempath使用,常用于无法从maven仓库获取的包。

<dependency><groupId>com.supermap</groupId><artifactId>data</artifactId><version>1.0</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/supermap/com.supermap.data.jar</systemPath>
</dependency>

import :表示为当前项目依赖为多继承关系,常用于项目中自定义父工程,需要注意的是只能用在dependencyManagement里面,且仅用于type=pom的dependency。

<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

参考:
https://www.jianshu.com/p/9d0c16532e2e
https://blog.csdn.net/weixin_38997187/article/details/107352518

springCloud中dependencyManagement、type、scope在父模块和子模块分别的作用相关推荐

  1. Maven 父pom中dependencyManagement版本优先级高于传递依赖版本

    当使用了传递依赖,也就是使用了没有显示声明的依赖时,如果继承的<dependencyManagement/>中声明了使用的传递依赖的版本,那么最终使用的依赖是<dependencyM ...

  2. BBS中父模块缩进,子模块归属父模块的实现方式

    BBS中父模块缩进,子模块归属父模块的实现方式 板块显示顺序问题 在tbl_board表中增加一个显示顺序字段 order alter table tbl_board add ( order_num ...

  3. Idea的Maven项目:子模块无法使用父模块中已导入的依赖问题

    转载:https://blog.csdn.net/iteacoder/article/details/109322386 问题描述 使用idea创建maven项目后,如果频繁地修改maven模块名称, ...

  4. SpringCloud学习记录(1)-父工程与子模块创建及子模块调用

    创建父工程,导入依赖 选择创建maven工程,步骤如下: 修改父工程的pom.xml,具体如下: <?xml version="1.0" encoding="UTF ...

  5. Maven中dependencyManagement标签和dependencies的区别

    今天在maven的pom文件中看到了dependencyManagement标签,用法如下: <dependencyManagement><dependencies><d ...

  6. cmenu 隐藏子项中的一个子项_区分Maven中dependencyManagement与dependencies的作用

    导读:使用maven是为了更好的帮项目管理包依赖,maven的核心就是pom.xml.而maven中有许多的标签,下面我们主要讨论parent.dependencies与dependencyManag ...

  7. springcloud中Gateway的限流熔断机制!

    前言 目前,Spring Cloud Gateway是仅次于Spring Cloud Netflix的第二个最受欢迎的Spring Cloud项目(就GitHub上的星级而言).它是作为Spring ...

  8. SpringCloud中Client向Eureka注册中心注册服务成功后不久就Unregistering(Unregistering application 服务名 with eureka with)

    在SpringCloud中Server端启动成功了,再去启动Client项目,可能会出现这样的问题,Console日志如下: 2022-06-22 16:04:41.990 INFO 14964 -- ...

  9. maven中强大的scope标签详解

    maven中强大的scope标签详解 本文目的   接上一篇maven的版本号version的总结及理解   当我在封装工具jar包的时候,发现有些依赖,是一定要在工具代码里使用的,比如我做的工具包里 ...

最新文章

  1. 平方的观测值表概率_中央气象台:“三九”大概率不会比“二九”更冷
  2. Oracle 安装错误 - 无法在节点xx上执行物理内存检查 的解决
  3. Android FragmentManage FragmentTransaction介绍
  4. 【Boost】boost库中thread多线程详解5——谈谈线程中断
  5. spring—Bean实例化三种方式
  6. 用CImage类来显示PNG JPG等图片
  7. StringUtils工具类说明
  8. 漫谈Google的Native Client(NaCl)技术(二)–技术篇(兼谈LLVM)
  9. Ubuntu MySQL 配置 time_zone
  10. python学习(四)----函数
  11. 2020年美赛C题(数据分析题)O奖论文笔记 (1)
  12. HTML——添加网页背景音乐
  13. 全文专利 PDF 免费下载
  14. 智能工厂仓库管理系统软件有哪些哪家好呢
  15. Mac修改hosts文件
  16. ERROR: Library projects cannot set applicationId. applicationId is set to 'com.example.baiduditu' in
  17. FX3U和三菱伺服控制的框架标准程序 回原点、JOG手动、绝对定位、相对定位、控制等部分
  18. 刚构桥的优缺点_桥梁钢结构特点及优缺点
  19. 二级域名解析配置方法
  20. 北华大学c语言题库百度云,北华大学C语言题库精简打印版(全).doc

热门文章

  1. Python二级试题(一)
  2. 【SQL】leetcode 584.寻找用户推荐人(知识点:SQL的三值逻辑)
  3. 游览器缓存和数据压缩
  4. 短视频平台盈利模式深度解析
  5. drawboard pdf拆分文件_Drawboard PDF(pfd处理软件)V5.5.20.1 最新版
  6. 小学计算机游戏小狐狸历险记,小狐狸历险记
  7. MAC地址、MAC地址表、端口安全、MAC地址漂移
  8. 「杂谈」面试中需要注意的非技术问题
  9. 飞机场100个常见公共标志英文单词(zt)
  10. WPS 查找历史版本记录