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-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 中不要重複引用 已有的依賴,否則會導致子模塊不能繼承父模塊依賴的情況出現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章