我们使用 BOM 在 MyCompany 中共享我们的依赖管理。
它被定义为 Maven POM。这是一个最小的例子:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.common</groupId>
<artifactId>common-java</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<spring-boot.version>2.4.0</spring-boot.version>
</properties>
<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>
</project>
然后在 Gradle 项目中使用它。
虽然这适用于代码依赖,但我想从插件管理中找到一种使用它的方法。
现在,我们在settings.gradle.kts
中定义:
pluginManagement {
val springBootVersion: String by settings
plugins {
id("org.springframework.boot") version(springBootVersion)
}
}
springBootVersion
在 gradle.properties
中定义。
这对我来说是个问题,因为 Spring 版本同时定义了:
- 在共享 BOM 中;
- 在每个项目的插件管理中。
我如何从 Gradle 的插件管理中访问该 BOM? 如果我做不到,那么不要重复自己的好习惯是什么?
最佳答案
您的自定义平台可以提供关于您希望客户使用哪个版本的 Spring Boot Gradle 插件的意见(特别是因为它不包含在 spring-boot-dependencies
BOM 中)。
以下是示例平台的 build.gradle.kts
文件的相关部分,例如:
plugins {
`java-platform`
}
javaPlatform {
allowDependencies()
}
dependencies {
// This platform extends the Spring Boot platform.
api(platform("org.springframework.boot:spring-boot-dependencies:2.7.6"))
constraints {
// Provide an opinion about which version of the Spring Boot Gradle plugin clients
// should use since it's not included in the standard spring-boot-dependencies BOM.
api("org.springframework.boot:spring-boot-gradle-plugin:2.7.6")
}
}
这将生成一个同时对齐 spring-boot-gradle-plugin
和 spring-boot-dependencies
的 BOM:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-gradle-plugin</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后,您的客户端项目可以仅依赖于您的平台,并使用如下方式继承其对 Spring Boot 版本的意见:
buildSrc/build.gradle.kts
:
// Pull in the version of the Spring Boot Gradle plugin specified by your
// platform, making it available to your regular build script.
dependencies {
implementation(enforcedPlatform("my-group:my-base-bom:1.0.0"))
implementation("org.springframework.boot:spring-boot-gradle-plugin")
}
build.gradle.kts
:
plugins {
id("org.springframework.boot") // version inherited from your platform
}
dependencies {
// It's necessary to specify it for each configuration.
implementation(enforcedPlatform("my-group:my-base-bom:1.0.0"))
// Pull in any normal Spring Boot-managed dependencies you need (versions come from platform).
implementation("org.springframework.boot:spring-boot-starter-web")
}
当然,你也可以使用 Gradle version catalogs集中版本,为了在示例中清楚起见,这些版本是内联的。
关于spring-boot - 使用来自 Gradle 插件管理的 BOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65309993/