【问题标题】:Using Gradle 5.1 "implementation platform" instead of Spring Dependency Management Plugin使用 Gradle 5.1“实现平台”而不是 Spring 依赖管理插件
【发布时间】:2019-06-01 08:25:02
【问题描述】:

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

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 是否正确,如果是这样,那么我是否遗漏了一些东西才能使其正常工作?

【问题讨论】:

    标签: spring-boot gradle dependencies maven-bom


    【解决方案1】:

    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 添加到多个配置中,具体取决于您的项目。

    【讨论】:

    • gradle 不支持覆盖版本并添加外部(未在 BOM 中声明的依赖项)依赖版本,对吗? (enforcedPlatform 不是我需要的)
    • github.com/gradle/gradle/issues/9160 - 好的,我们必须等待 :)
    猜你喜欢
    • 2019-01-30
    • 2020-09-27
    • 2017-11-15
    • 2022-12-23
    • 1970-01-01
    • 2018-08-24
    • 2019-10-26
    • 2017-09-29
    • 2013-06-15
    相关资源
    最近更新 更多