Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 856
yuppie-flu Avatar asked Mar 05 '20 16:03

yuppie-flu


1 Answers

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")
    }
}
like image 99
afterburner Avatar answered Jan 03 '23 01:01

afterburner