如何将Java平台中指定的版本添加到所有相关配置中?

问题描述 投票:0回答:0

我已经将我们的项目从 Spring 依赖管理插件转换为使用 Java 平台子项目来修复项目范围内的依赖版本。

在大多数情况下,这很容易。 但是,Spring 依赖管理会自动将其版本应用于所有相关配置。 使用 Gradle 原生机制,我需要手动将平台添加到所有相关的依赖配置(我们项目中大约有 30 个,并且还在不断增加)。

如何避免将平台依赖添加到每个配置?

之前,我们在根级别有这个

build.gradle

plugins {
    id 'io.spring.dependency-management'
}

allprojects {
    apply plugin: 'io.spring.dependency-management'
    plugins.with {
        withType(JavaPlugin) {
            dependencyManagement {
                // ... version specifications, both importing Maven
                // BOMs and settings individual versions ...

使用 Java 平台方法,我最终得到了

allprojects {
    plugins.with {
        withType(JavaPlugin) {
            dependencies {
                // For some reasons it confuses these plugins if we
                // add our "gradle bom" to them (but then codenarc
                // needs it)
                def excludedConfigurations = [
                    'checkstyle', 'pmd', 'jacocoAgent', 'spotbugs',
                    'spotbugsPlugins', 'spotbugsSlf4j', 'jacocoAnt',
                    // ... about a ten more
                ] as Set
                configurations.all { conf ->
                    if(!(conf.name in excludedConfigurations)) {
                        add(conf.name,
                            enforcedPlatform(project(":acme-dependencies")))
                    }
                }

我们的项目中有很多依赖项,一些来自插件(比如来自 JAXB 插件的 xjc)和一些自定义 配置(例如,在构建期间使用单独的类路径运行工具或用于战争覆盖),因此指定 “包含”配置而不是上面代码示例中的“排除”配置需要更长的列表,这也是 更不稳定。

gradle dependency-management
© www.soinside.com 2019 - 2024. All rights reserved.