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. Windows下完成端口移植Linux下的epoll
  3. 知识图谱最新论文清单,高阶炼丹师为你逐一解读
  4. 开源搜索引擎solr4.0+tomcat7实现中文分词
  5. 泛函编程(4)-深入Scala函数类
  6. RedHat7配置本地yum源(超详细过程)
  7. 计算机组策略无法编辑,win7系统无法打开本地组策略编辑器的解决方法
  8. chrome误删书签恢复。
  9. mysql全文索引详解_MySql全文索引详解
  10. 【Airflow踩坑】XCom大数据传递反序列化失败
  11. javaScript-力扣-题库-11. 盛最多水的容器
  12. JavaScript Promise迷你书(中文版)
  13. 面对这个缓慢、脆弱、健忘的互联网,IPFS协议势在必行!
  14. 【linux】查看服务器的GPU 谁(用户)在使用
  15. 【Linux】系统安装
  16. 教程 | 校徽头像制作小程序后端实现
  17. 求y=sinx反函数的导数
  18. 哈佛大学搞出声波传数据芯片,抗干扰能力更强,适用于量子计算等新兴领域...
  19. 极速空间笔记本CPU天梯图(笔记本CPU性能排行)——跟小虫学电脑配置
  20. 基于JSP校园一卡通管理系统

热门文章

  1. 论文查重自己文章会查吗?
  2. 数据结构:区域染色问题
  3. 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数), 凡报到3的人退出圈子,问最后留下的是原来第几号的那位。(*)
  4. Kibana使用(Discover):数据的查询
  5. 软件工程面向对象方法、Coad、Booch、OMT、UML方法
  6. Java 面试题大集合,2019最新最常见面试题加答案
  7. [国家集训队2012]tree(陈立杰)
  8. App安全架构之前端安全防护
  9. 计算机键盘space键在哪,space是哪个键,详细教您space是哪个键
  10. 抖音的上下滑实现—iOS