Maven多模块开发SpringBoot项目自定义第三方依赖版本
参考:官方文档 - Build System of Maven
https://blog.didispace.com/books/spring-boot-reference/IX. ‘How-to’ guides/80.3 Customize dependency versions.html
对于 SpringBoot 使用 Maven 构建项目做依赖管理大致分下面两种情况,一种是以 spring-boot-starter-parent 作为父模块,属于默认的方式;另一种是不用 Parent POM 来构建项目,本文着重介绍第二种方式来修改依赖版本的方法。
spring-boot-starter-parent 分析
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
- 进入
spring-boot-starter-parent
,可以看到 spring-boot-dependencies 做父依赖管理;
- 进入
spring-boot-dependencies
, 这里有 SpringBoot 以及常用第三方依赖的版本信息,默认引入其他依赖会使用这里的版本定义。
第三方依赖版本修改
现在我们可以看到 spring-boot-dependencies
中定义了很多第三方依赖有版本,下面介绍如何修改这些版本。
- 注意:每个Spring Boot发布都是基于一些特定的第三方依赖集进行设计和测试的,覆盖版本可能导致兼容性问题。
- Gradle中为了覆盖依赖版本,你需要指定如下所示的version:ext['slf4j.version'] = '1.7.5'
1. 继承自 spring-boot-dependencies
也就是说在pom 定义时的父模块必须直接或间接继承自 spring-boot-dependencies
如 pom.xml 定义了:(当前版本的Elasticsearch版本为6.8.6)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
当我们需要修改版本信息时,只需要在 pom 文件中重写 properties 属性
<properties>
<elasticsearch.version>7.4.2</elasticsearch.version>
</properties>
注意:
- 这里
<properties>
这种定义属性名要和spring-boot-dependencies
中属性名一样。 - 这里是 Maven 项目继承(直接或间接)自
spring-boot-starter-dependencies
才有效。
2. dependencyManagement 引用 spring-boot-dependencies
也就是说,项目中使用了 <scope>import</scope>
,将 spring-boot-dependencies
添加到自己的 dependencyManagement
片段。
如 pom.xml 定义了:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
这种情况下修改版本,则需要在 pom 文件中重新定义要修改版本依赖的 artifact 而不是覆盖属性版本信息。
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
问题来源:尚硅谷-谷粒商城 Elasticsearch 项目文档
spring-boot-starter-parent 补充
参考官方文档 :
Maven users can inherit from the spring-boot-starter-parent project to
obtain sensible defaults. The parent project provides the following
features:
Java 1.8 as the default compiler level.
UTF-8 source encoding.
A Dependency Management section, inherited from the
spring-boot-dependencies pom, that manages the versions of common
dependencies. This dependency management lets you omit tags
for those dependencies when used in your own pom.An execution of the repackage goal with a repackage execution id.
Sensible resource filtering.
Sensible plugin configuration (exec plugin, Git commit ID, and shade).
Sensible resource filtering for application.properties and
application.yml including profile-specific files (for example,
application-dev.properties and application-dev.yml)Note that, since the application.properties and application.yml files
accept Spring style placeholders (${…}), the Maven filtering is
changed to use @..@ placeholders. (You can override that by setting a
Maven property called resource.delimiter.)最后一段例子:Maven filtering 使用
spring-boot-starter-parent 默认使用 jdk 1.8、UTF-8 编码和一些依赖的版本控制,所以在项目中的 dependencyManagement
中不要重复引用 已有的依赖,否则会导致子模块不能继承父模块依赖的情况出现。
Maven多模块开发SpringBoot项目自定义第三方依赖版本的更多相关文章
- Spring Boot 的Maven多模块开发web项目使用外部容器进行部署
Spring Boot中自带有Tomcat容器,因此Spring Boot项目只需要运行main函数,就可以运行,但是以往的web项目,我们习惯于使用自己安装的Tomcat运行或者使用Tomcat.J ...
- spring+springmvc+hibernate架构、maven分模块开发样例小项目案例
maven分模块开发样例小项目案例 spring+springmvc+hibernate架构 以用户管理做測试,分dao,sevices,web层,分模块开发測试!因时间关系.仅仅測查询成功.其它的准 ...
- 如何构建多模块的SpringBoot项目
通过阅读本文你将了解到:如何将已有SpringBoot项目改成多模块 & 如何新构建多模块SpringBoot项目 以下示例基于我正在使用的order(订单服务)进行演示,无论你用的是什么项目 ...
- Kotlin开发springboot项目(三)
Kotlin开发springboot项目(三) 在线工具 https://www.sojson.com IDEA中Kotlin生成可执行文件1,项目使用Gradle构建2,在model的build.g ...
- Kotlin开发springboot项目(二)
Kotlin开发springboot项目(二) 中文学习网站: https://www.kotlincn.net/ 研究一下kotlin的demo: https://github.com/JetBra ...
- Kotlin开发springboot项目(一)
Kotlin开发springboot项目(一) Kotlin语言与Xtend语言有很多相似之处 为什么会存在这么多JVM语言? 现存的语言提供了太过受限制的功能,要不就是功能太过繁杂,导致语言的臃肿和 ...
- 分享vs低版本开发的项目到VS高版本时遇到的4个小问题解决之记录
分享vs低版本开发的项目到VS高版本时遇到的4个小问题解决之记录 原文首发: http://anforen.com/wp/2017/08/extensionattribute_compilerserv ...
- SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、yml属性配置、多环境配置、自定义properties配置
1 IntelliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到 ...
- SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、属性配置、多环境配置
1 InteliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到官 ...
随机推荐
- AI框架类FAQ
AI框架类FAQ 数据处理 问题:如何在训练过程中高效读取数量很大的数据集? 答复:当训练时使用的数据集数据量较大或者预处理逻辑复杂时,如果串行地进行数据读取,数据读取往往会成为训练效率的瓶颈.这种情 ...
- spring如何集成第三方框架? 比如mybatis
实体Bean的创建: 1: 基于class构建, 2: 构造方法构建 3: 静态工厂方法创建 4: FactoryBean构建 spring如何集成第三方框架? 比如mybatis 在mybatis中 ...
- teprunner重磅更新Git打通PyCharm与测试平台
经过Python测试交流群的小伙伴群策群力,teprunner添加了一个重要功能,把PyCharm中的代码,通过Git同步到测试平台中,生成测试用例.这样,teprunner就成了一个名副其实的pyt ...
- 网页站点下载器teleport ultra
软件名称:teleport ultra 介绍:teleport ultra是一款专门的网页站点下载器,使用这款工具可以方便地下载网页数据,包括网站的文字.图片.flash动画等,可以轻松下载所有的网站 ...
- k8s-记一次安全软件导致镜像加载失败
近期在现场项目中遇到了一个镜像加载失败的问题,相关报错如下: Error processing tar file(exit status 1): symlink . /usr/bin/X11: per ...
- 十亿级流量下,我与Redis时延小突刺的战斗史
一.背景 某一日收到上游调用方的反馈,提供的某一个Dubbo接口,每天在固定的时间点被短时间熔断,抛出的异常信息为提供方dubbo线程池被耗尽.当前dubbo接口日请求量18亿次,报错请求94W/天, ...
- 解Bug之路-ZooKeeper集群拒绝服务
解Bug之路-ZooKeeper集群拒绝服务 前言 ZooKeeper作为dubbo的注册中心,可谓是重中之重,线上ZK的任何风吹草动都会牵动心弦.最近笔者就碰到线上ZK Leader宕机后,选主无法 ...
- 【模拟8.09】建设城市(city) (容斥)
放在了考试T1 发现70分的DP很水啊,f[i][j]为当前位置是i分配了j个队的方案 我们用前缀和统计,在将i删去,j倒序枚举,就可以删掉一维(也可以滚动数组滚起来) 1 #include<i ...
- DNS 解析过程
DNS 是应用层协议,用于将域名转换成 IP 地址. 1. 解析过程 DNS 的核心系统是一个三层的树状.分布式服务,基本对应域名的结构. 根域名服务器:管理顶级域名服务器,返回 com.net.cn ...
- excel VBA根据单元格内的逗号把内容拆分行
Sub test1() Dim h Dim j As Integer j = 0 '用于辅助循环的进行,可以在拆分行获取下一个需要拆分单元格的行号 'Application. ...