简体   繁体   English

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

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

There is a Gradle 6.X multi-module project using Kotlin DSL.有一个使用 Kotlin DSL 的 Gradle 6.X 多模块项目。 buildSrc feature is used to manage dependency versions in a central place. buildSrc功能用于在中央位置管理依赖项版本。 Something similar to the approach described here .类似于此处描述的方法。

The project uses an internal server to download dependencies.该项目使用内部服务器下载依赖项。 It causes the duplication of the repository settings configuration in two places:它会导致两个地方的存储库设置配置重复:

buildSrc/build.gradle.kts : 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
        }
    }
}

and root settings.gradle.kts :和根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
                }
            }
        }
    }
}
...

Is it possible somehow to share the duplicated maven block between these two places?是否有可能以某种方式在这两个地方之间共享重复的maven块?

You could try refactoring your kts file into something like this.您可以尝试将kts文件重构为这样的内容。 Does this help you?这对你有帮助吗?

repositories.gradle.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")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM