Hi,
is there a way to define a function in settings.gradle.kts
that is usable within the pluginManagement
, dependencyResolutionManagement
and toolchainManagement
block?
Use-Case:
I have some duplicate code within these three blocks to read our maven repo properties from env and set them for each repo. I would like to reuse the code.
I would prefer to use a function authenticate
to do this.
Unfortunately, I get an error Unresolved reference: authenticate
pluginManagement {
repositories {
maven {
url = uri("http://example.com")
authenticate(this)
}
}
}
dependencyResolutionManagement {
repositories {
maven {
url = uri("http://example.com")
authenticate(this)
}
}
}
toolchainManagement {
jvm {
javaRepositories {
repository("maven") {
resolverClass.set(JavaToolchainResolver::class.java)
authenticate(this)
}
}
}
}
fun authenticate(
authenticationSupported: AuthenticationSupported
) {
val MAVEN_USER = "MAVEN_USER"
val MAVEN_PASSWORD = "MAVEN_PASSWORD"
val mavenUser = System.getenv(MAVEN_USER)
if (mavenUser.isNullOrBlank()) {
throw GradleException("$MAVEN_USER is not set in environment")
}
val mavenPassword = System.getenv(MAVEN_PASSWORD)
if (mavenPassword.isNullOrBlank()) {
throw GradleException("$MAVEN_PASSWORD is not set in environment")
}
authenticationSupported.credentials {
username = mavenUser
password = mavenPassword
}
}