管理相依性

若要在您的 Spring Boot 應用程式中管理相依性,您可以套用 io.spring.dependency-management 外掛程式,或使用 Gradle 原生的 BOM 支援。前者的主要優點是它提供基於屬性的受管理版本自訂,而後者可能會產生更快的建置速度。

使用 Dependency Management 外掛程式管理相依性

當您套用 io.spring.dependency-management 外掛程式時,Spring Boot 的外掛程式將自動從您使用的 Spring Boot 版本匯入 spring-boot-dependencies BOM。這提供了與 Maven 使用者所享有的類似的相依性管理體驗。例如,它允許您在宣告 BOM 中管理的相依性時省略版本號碼。若要使用此功能,請以通常的方式宣告相依性,但省略版本號碼

  • Groovy

  • Kotlin

dependencies {
	implementation('org.springframework.boot:spring-boot-starter-web')
	implementation('org.springframework.boot:spring-boot-starter-data-jpa')
}
dependencies {
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("org.springframework.boot:spring-boot-starter-data-jpa")
}

自訂受管理版本

在套用 Dependency Management 外掛程式時自動匯入的 spring-boot-dependencies BOM 使用屬性來控制其管理的相依性版本。瀏覽 Spring Boot 參考中的相依性版本屬性章節,以取得這些屬性的完整清單。

若要自訂受管理版本,您可以設定其對應的屬性。例如,若要自訂由 slf4j.version 屬性控制的 SLF4J 版本

  • Groovy

  • Kotlin

ext['slf4j.version'] = '1.7.20'
extra["slf4j.version"] = "1.7.20"
每個 Spring Boot 版本都針對一組特定的第三方相依性進行設計和測試。覆寫版本可能會導致相容性問題,應謹慎操作。

隔離使用 Spring Boot 的相依性管理

Spring Boot 的相依性管理可以在專案中使用,而無需將 Spring Boot 的外掛程式套用於該專案。SpringBootPlugin 類別提供了一個 BOM_COORDINATES 常數,可用於匯入 BOM,而無需知道其群組 ID、Artifact ID 或版本。

首先,設定專案相依於 Spring Boot 外掛程式,但不要套用它

  • Groovy

  • Kotlin

plugins {
	id 'org.springframework.boot' version '3.3.5' apply false
}
plugins {
	id("org.springframework.boot") version "3.3.5" apply false
}

Spring Boot 外掛程式對 Dependency Management 外掛程式的相依性表示您可以使用 Dependency Management 外掛程式,而無需宣告對它的相依性。這也表示您將自動使用與 Spring Boot 使用的相同版本的 Dependency Management 外掛程式。

套用 Dependency Management 外掛程式,然後將其設定為匯入 Spring Boot 的 BOM

  • Groovy

  • Kotlin

apply plugin: 'io.spring.dependency-management'

dependencyManagement {
	imports {
		mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
	}
}
apply(plugin = "io.spring.dependency-management")

the<DependencyManagementExtension>().apply {
	imports {
		mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
	}
}

上面的 Kotlin 程式碼有點笨拙。那是因為我們使用的是命令式方法來套用 Dependency Management 外掛程式。

我們可以透過從根父專案套用外掛程式,或使用 plugins 區塊(如同我們對 Spring Boot 外掛程式所做的那樣)來使程式碼不那麼笨拙。此方法的缺點是它強制我們指定 Dependency Management 外掛程式的版本

plugins {
	java
	id("org.springframework.boot") version "3.3.5" apply false
	id("io.spring.dependency-management") version "1.1.6"
}

dependencyManagement {
	imports {
		mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
	}
}

了解更多

若要深入了解 Dependency Management 外掛程式的功能,請參閱其文件

使用 Gradle 的 BOM 支援管理相依性

Gradle 允許透過將 BOM 宣告為 platformenforcedPlatform 相依性,來使用 BOM 管理專案的版本。platform 相依性將 BOM 中的版本視為建議,而相依性圖中的其他版本和約束可能會導致使用與 BOM 中宣告的版本不同的相依性版本。enforcedPlatform 相依性將 BOM 中的版本視為要求,它們將覆寫相依性圖中找到的任何其他版本。

SpringBootPlugin 類別提供了一個 BOM_COORDINATES 常數,可用於宣告對 Spring Boot 的 BOM 的相依性,而無需知道其群組 ID、Artifact ID 或版本,如下例所示

  • Groovy

  • Kotlin

dependencies {
	implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
dependencies {
	implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
}

平台或強制平台將僅約束已宣告它的組態或從已宣告它的組態擴展而來的組態的版本。因此,可能需要在多個組態中宣告相同的相依性。

自訂受管理版本

使用 Gradle 的 BOM 支援時,您無法使用 spring-boot-dependencies 中的屬性來控制其管理的相依性版本。相反,您必須使用 Gradle 提供的機制之一。其中一種機制是解析策略。SLF4J 的模組都在 org.slf4j 群組中,因此它們的版本可以透過將該群組中的每個相依性設定為使用特定版本來控制,如下例所示

  • Groovy

  • Kotlin

configurations.all {
	resolutionStrategy.eachDependency { DependencyResolveDetails details ->
		if (details.requested.group == 'org.slf4j') {
			details.useVersion '1.7.20'
		}
	}
}
configurations.all {
    resolutionStrategy.eachDependency {
        if (requested.group == "org.slf4j") {
            useVersion("1.7.20")
        }
    }
}
每個 Spring Boot 版本都針對一組特定的第三方相依性進行設計和測試。覆寫版本可能會導致相容性問題,應謹慎操作。