I'm using Gradle Kotlin DSL. I want to know whether it's possible to read Gradle properties in the settings.gradle.kts
file?
I have gradle.properties
file like this:
nexus_username=something
nexus_password=somepassword
I've tried the following but still it can't read the properties:
dependencyResolutionManagement {
repositories {
maven {
setUrl("https://some.repository/")
credentials {
val properties =
File(System.getProperty("user.home")+"\\.gradle", "gradle.properties").inputStream().use {
java.util.Properties().apply { load(it) }
}
username = properties["nexus_username"].toString()
password = properties["nexus_password"].toString()
}
}
}
}
You can access values set in gradle.properties
in both build.gradle.kts
and settings.gradle.kts
using delegate properties (Kotlin DSL only, because delegate properties is a Kotlin feature!).
gradle.properties
kotlin.code.style=official
# Your values here
testValue=coolStuff
build.gradle.kts
val testValue: String by project
settings.gradle.kts
val testValue: String by settings
You can access gradle parameters using providers (since 6.2)
val usernameProvider = providers.gradleProperty("nexus_username")
val passwordProvider = providers.gradleProperty("nexus_password")
dependencyResolutionManagement {
repositories {
maven {
setUrl("https://some.repository/")
credentials {
username = usernameProvider.getOrNull()
password = passwordProvider.getOrNull()
}
}
}
}
To work on Groovy, you need to replace the variable declaration with:
def usernameProvider = providers.gradleProperty("nexus_username")
def passwordProvider = providers.gradleProperty("nexus_password")
Based on answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With