Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read Gradle properties in the 'settings.gradle.kts' file?

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()
            }
        }
    }
}
like image 900
Jimly Asshiddiqy Avatar asked Sep 02 '25 17:09

Jimly Asshiddiqy


2 Answers

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
like image 180
Bradley Thompson Avatar answered Sep 05 '25 06:09

Bradley Thompson


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

like image 28
Михаил Иванов Avatar answered Sep 05 '25 05:09

Михаил Иванов



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!