8

There is a Gradle 6.X multi-module project using Kotlin DSL. buildSrc feature is used to manage dependency versions in a central place. 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:

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:

...
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?

yuppie-flu
  • 1,270
  • 9
  • 13

1 Answers1

10

You could try refactoring your kts file into something like this. Does this help you?

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")
    }
}
afterburner
  • 2,552
  • 13
  • 21