参考:官方文档 - 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 来构建项目,本文着重介绍第二种方式来修改依赖版本的方法。html
<!-- 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
也就是说在pom 定义时的父模块必须直接或间接继承自 spring-boot-dependencies
app
如 pom.xml 定义了:(当前版本的Elasticsearch版本为6.8.6)elasticsearch
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent>
当咱们须要修改版本信息时,只须要在 pom 文件中重写 properties 属性maven
<properties> <elasticsearch.version>7.4.2</elasticsearch.version> </properties>
注意:ide
<properties>
这种定义属性名要和 spring-boot-dependencies
中属性名同样。spring-boot-starter-dependencies
才有效。也就是说,项目中使用了 <scope>import</scope>
,将 spring-boot-dependencies
添加到本身的 dependencyManagement
片断。spring-boot
如 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 而不是覆盖属性版本信息。ui
<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 项目文档编码
参考官方文档 :
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 omittags
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.八、UTF-8 编码和一些依赖的版本控制,因此在项目中的 dependencyManagement
中不要重复引用 已有的依赖,不然会致使子模块不能继承父模块依赖的状况出现。