使用Gradle 5.1“实现平台”代替Spring Dependency Management插件

Sha*_*att 5 dependencies byte-order-mark gradle spring-boot

我编写了一个Gradle插件,其中包含许多常用的设置配置,因此我们所有的项目都只需要应用该插件和一组依赖项即可。它使用Spring Dependency Management插件为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"
        }
     }
  }
Run Code Online (Sandbox Code Playgroud)

尽管在Gradle 5.1中仍然可以使用,但我想用BOM导入的新依赖机制替换Spring Dependency Management插件,所以我将上面的内容更新为:

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

        project.dependencies.platform("org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE")
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,更改意味着没有导入由这些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插件是正确的,如果是的话,我是否错过了一些使它起作用的东西?

Lou*_*met 5

Gradle 5中的平台支持可以代替Spring依赖管理插件来消耗BOM。但是,Spring插件提供了Gradle支持未涵盖的功能。

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

project.dependencies.platform("org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE")
Run Code Online (Sandbox Code Playgroud)

这只会创建一个Dependency,仍然需要将其添加到配置中。通过执行以下操作:

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

其中configurationName是需要BOM的配置的名称。请注意,您可能必须将此BOM添加到多种配置中,具体取决于您的项目。