Using Gradle 5.1 “implementation platform” instead of Spring Dependency Management Plugin

我们两清 提交于 2020-12-08 07:18:55

问题


I have written a Gradle Plugin that contains a bunch of common setup configuration so that all of our projects just need to apply that plugin and a set of dependencies. It uses the Spring Dependency Management Plugin to setup the BOM imports for Spring as shown in the code snippet below:

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"
        }
     }
  }

Whilst that still works in Gradle 5.1 I wanted to replace the Spring Dependency Management Plugin with the new dependency mechanism for BOM Imports so I updated the above to now be this:

trait ConfigureDependencyManagement {
    void configureDependencyManagement(final Project project) {
        assert project != null

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

Unfortunately that change means none of the dependencies defined by these BOMs are being imported and I get errors like these when building projects?

Could not find org.springframework.boot:spring-boot-starter-web:. Required by: project :

Could not find org.springframework.boot:spring-boot-starter-data-jpa:. Required by: project :

Could not find org.springframework.boot:spring-boot-starter-security:. Required by: project :

Am I correct in thinking the Spring Dependency Management Plugin is no longer needed with Gradle 5.1 and if so then am I missing something for this to work?


回答1:


The platform support in Gradle 5 can replace the Spring dependency management plugins for BOM consumption. However the Spring plugin offers features that are not covered by the Gradle support.

Regarding your issue, the problem comes from the following line:

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

This will simply create a Dependency, it still needs to be added to a configuration. by doing something like:

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

where configurationName is the name of the configuration that requires the BOM. Note that you may have to add this BOM to multiple configurations, depending on your project.



来源:https://stackoverflow.com/questions/54061391/using-gradle-5-1-implementation-platform-instead-of-spring-dependency-manageme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!