Maven多模块开发SpringBoot项目自定义第三方依赖版本

摘要:
--InheritdefaultsfromSpringBoot--˃org.springframework.bootspring-boot-starter-parent2.2.5.RELEASE进入spring-boot-starter-parent,可以看到spring-boot-dependencies做父依赖管理;进入spring-boot-dependencies,这里有SpringBoot以及常用第三方依赖的版本信息,默认引入其他依赖会使用这里的版本定义。第三方依赖版本修改现在我们可以看到spring-boot-dependencies中定义了很多第三方依赖有版本,下面介绍如何修改这些版本。这里是Maven项目继承自spring-boot-starter-dependencies才有效。

参考:官方文档 - 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-starter-parent
  • 进入 spring-boot-dependencies, 这里有 SpringBoot 以及常用第三方依赖的版本信息,默认引入其他依赖会使用这里的版本定义。
    spring-boot-dependencies
第三方依赖版本修改

现在我们可以看到 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项目自定义第三方依赖版本》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇RocketMQ集群搭建及安装rocketmq-console命令行浏览器汇总下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

玩客云安装Armbian

首先,轻扫uboot。阿里云的网卡非常痛苦。如果您没有滑动特定的uboot,则无法驱动它。有三个命令可以滑动uboot,可以按顺序执行:usbstart;fatloadusb01200000u引导。binstorerom_write12000000060000 saveenv三个命令执行后,关闭并重新启动Play Keyun一次,然后再次进入中断模式。再次插...

快速打造 react 后台管理系统

前言我相信许多小伙伴可能会满足开发后台管理系统的要求,那么我们如何快速完成这一要求?本文将记录以React为起点创建基本管理系统模板的过程,以加深对React技术堆栈和项目实践的理解。我希望开发这样的项目对每个人都有帮助。如果文章中有错误和错误,也请看小伙伴的评论。提前感谢您的项目介绍。React admin是基于R快速创建React应用脚手架而构建的...

react 中echarts-for-react使用 自适应div

import*asReactfrom'react'importReactEchartsfrom'charts-for-ract'导出接口IProps{}接口IState{}classCapitalBudgetsextendsReact.Component<IProps,IState>{constructor(props:IProps){super(props...

为服务中网关的作用

“API网关”核心组件是架构用于满足此些需求。API网关定位为应用系统服务接口的网关,区别于网络技术的网关,但是原理则是一样。API网关统一服务入口,可方便实现对平台众多服务接口进行管控,对访问服务的身份认证、防报文重放与防数据篡改、功能调用的业务鉴权、响应数据的脱敏、流量与并发控制,甚至基于API调用的计量或者计费等等。...

Github仓库重命名

1.在Github上重命名仓库,转到您自己的仓库,找到Setting标记,然后单击Options中的Settings以设置Repositoryname。2.修改本地仓库信息。由于远程仓库名称已更改,因此本地对应的仓库名称也应更改。1.检查当前远程仓库的信息$gitremote-v列出了所有远程仓库信息,包括网站地址。2.修改本地对应远程仓库的地址。修改后,使...

当微信小程序遇到AR(二)

当微信小程序遇到AR,会擦出怎么样的火花?期待与激动......通过该教程,可以从基础开始打造一个微信小程序的AR框架,所有代码开源,提供大家学习。注册地址=˃注册成功之后,需要下载微信小程序开发工具。下载地址=˃目前笔者的开发环境是:Windows10下载的微信小程序版本为:RCv1.0.2.1909111 打开,微信开发者工具之后,会看到如下的页面。...