參考:官方文檔 - 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
中不要重複引用 已有的依賴,不然會致使子模塊不能繼承父模塊依賴的狀況出現。