管理依賴項

要在你的 Spring Boot 應用程式中管理依賴,你可以應用 io.spring.dependency-management 外掛,或者使用 Gradle 的原生 BOM 支援。前者的主要優點是它提供了基於屬性的管理版本自定義功能,而後者可能會帶來更快的構建速度。

使用依賴管理外掛管理依賴

當你應用 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")
}

自定義管理版本

在應用依賴管理外掛時自動匯入的 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,而無需知道其 groupId、artifactId 或版本。

首先,將專案配置為依賴 Spring Boot 外掛,但不要應用它

  • Groovy

  • Kotlin

/*
 * Copyright 2012-present the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

Spring Boot 外掛對依賴管理外掛的依賴意味著你可以使用依賴管理外掛,而無需宣告對其的依賴。這也意味著你將自動使用與 Spring Boot 相同的依賴管理外掛版本。

應用依賴管理外掛,然後配置它以匯入 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 程式碼有點笨拙。那是因為我們正在使用命令式的方式應用依賴管理外掛。

我們可以透過從根父專案應用外掛,或者像我們對 Spring Boot 外掛那樣使用 plugins 塊來使程式碼不那麼笨拙。這種方法的缺點是它強制我們指定依賴管理外掛的版本

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

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

瞭解更多

要了解更多關於依賴管理外掛的功能,請參閱其文件

使用 Gradle 的 BOM 支援管理依賴

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

SpringBootPlugin 類提供了一個 BOM_COORDINATES 常量,可用於宣告對 Spring Boot BOM 的依賴,而無需知道其 group 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 版本都是針對一套特定的第三方依賴進行設計和測試的。覆蓋版本可能會導致相容性問題,應謹慎操作。
© . This site is unofficial and not affiliated with VMware.