8

我编写了一个 Gradle 插件,其中包含一堆常见的设置配置,因此我们所有的项目只需要应用该插件和一组依赖项。它使用 Spring Dependency Management Plugin 为 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-data-jpa:。要求:项目:

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

我认为 Gradle 5.1 不再需要 Spring Dependency Management Plugin 是否正确,如果是这样,那么我是否遗漏了一些东西来使它工作?

4

1 回答 1

8

Gradle 5 中的平台支持可以替换 Spring 依赖管理插件以进行 BOM 消耗。然而,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 添加到多个配置中,具体取决于您的项目。

于 2019-01-08T13:02:54.290 回答