Issue
So I'm using Gradle Kotlin DSL, I want to know if it's possible to read gradle properties inside settings.gradle.kts
?
I have gradle.properties
file like this:
nexus_username=something
nexus_password=somepassword
I've done it like this, but still can't read the properties.
dependencyResolutionManagement {
repositories {
mavenCentral()
google()
maven { setUrl("https://developer.huawei.com/repo/") }
maven { setUrl("https://jitpack.io") }
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()
}
}
}
}
Solution
After googling for a while, looking for answer. To keep those variable from being pushed into the repository It's best to use environment variable rather than gradle.properties
.
Simply because in settings.gradle.kts
file doesn't have function to call properties. And just to force reading public gradle.properties
file is such a hussle.
After setting your environment variables, you can call that variable in settings.gradle.kts
like this:
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { setUrl("https://jitpack.io") }
maven {
setUrl(System.getenv("nexus_url"))
credentials {
username = System.getenv("nexus_username")
password = System.getenv("nexus_password")
}
}
}
}
The variable will not be pushed to the repository, using it this way also helps you in CI/CD like github action, where you can just set the variable as secret.
Answered By - Jimly Asshiddiqy
Answer Checked By - Cary Denson (JavaFixing Admin)