使用 Gradle 5.1 "implementation platform" 而不是 Spring 依赖管理插件



我编写了一个Gradle插件,其中包含一组常见的设置配置,因此我们所有的项目都只需要应用该插件和一组依赖项。它使用Spring依赖关系管理插件来设置Spring的BOM导入,如下面的代码片段所示:

trait ConfigureDependencyManagement {
void configureDependencyManagement(final Project project) {
assert project != null
project.apply(plugin: "io.spring.dependency-management")
final DependencyManagementExtension dependencyManagementExtension = project.extensions.findByType(DependencyManagementExtension)
dependencyManagementExtension.imports {                 
mavenBom "org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE"
}
}
}

虽然这在Gradle 5.1中仍然有效,但我想用BOM导入的新依赖机制取代Spring依赖管理插件,所以我更新了上面的内容,现在是这样的:

trait ConfigureDependencyManagement {
void configureDependencyManagement(final Project project) {
assert project != null
project.dependencies.platform("org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE")
}
}

不幸的是,这一变化意味着这些BOM定义的依赖项都没有被导入,而且我在构建项目时会遇到这样的错误?

找不到org.springframework.boot:spring-boot starter web:。要求:项目:

找不到org.springframework.boot:spring-boot starter数据jpa:。要求:项目:

找不到org.springframework.boot:spring-boot启动器安全性:。要求:项目:

我认为Gradle 5.1不再需要Spring依赖关系管理插件是正确的吗?如果是,那么我是否缺少了一些东西来实现这一点?

Gradle 5中的平台支持可以取代BOM消耗的Spring依赖关系管理插件。然而,Spring插件提供了Gradle支持中未涵盖的功能。

关于您的问题,问题来自以下行:

project.dependencies.platform("org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE")

这只会创建一个Dependency,它仍然需要添加到配置中。通过做一些类似的事情:

def platform = project.dependencies.platform("org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE")
project.dependencies.add("configurationName", platform)

其中configurationName是需要BOM的配置的名称。请注意,根据您的项目,您可能需要将此BOM表添加到多个配置中。

最新更新