Gradle:在 settings.gradle.kts 和 buildSrc/build 之间共享存储库配置。gradle.kts

Gradle: share repository configuration between settings.gradle.kts and buildSrc/build.gradle.kts

有一个 Gradle 6.X 使用 Kotlin DSL 的多模块项目。 buildSrc 特性用于集中管理依赖版本。类似于 here.

中描述的方法

该项目使用内部服务器下载依赖项。 它导致在两个地方重复存储库设置配置:

buildSrc/build.gradle.kts:

plugins {
    `kotlin-dsl`
}

repositories {
    // The org.jetbrains.kotlin.jvm plugin requires a repository
    // where to download the Kotlin compiler dependencies from.
    maven {
        url = uri("${extra.properties["custom.url"] as? String}")
        credentials() {
            username = extra.properties["custom.username"] as? String
            password = extra.properties["custom.password"] as? String
        }
    }
}

和根settings.gradle.kts:

...
gradle.projectsLoaded {
    allprojects {
        repositories {
            maven {
                url = uri("${extra.properties["custom.url"] as? String}")
                credentials() {
                    username = extra.properties["custom.username"] as? String
                    password = extra.properties["custom.password"] as? String
                }
            }
        }
    }
}
...

是否有可能以某种方式在这两个地方之间共享重复的 maven 块?

您可以尝试将 kts 文件重构为类似这样的文件。这对你有帮助吗?

repositories.gradle.kts:

repositories {
            maven {
                url = uri("${extra.properties["custom.url"] as? String}")
                credentials() {
                    username = extra.properties["custom.username"] as? String
                    password = extra.properties["custom.password"] as? String
                }
            }
        }

buildSrc/build.gradle.kts

plugins {
    `kotlin-dsl`
}
apply(from="../repositories.gradle.kts")

settings.gradle.kts

gradle.projectsLoaded {
    allprojects {
        apply(from = "repositories.gradle.kts")
    }
}