How do I set global Gradle properties in an init script?

Viewed 959

I would like to move company-specific configuration out of my ~/.gradle/gradle.properties and into an init script, ~/.gradle/company.init.gradle. Unfortunately, I can't figure out how to defien global Gradle properties in a init script in the same way that I would do so in a gradle.properties file.

My ~/.gradle/gradle.properties currently has:

org.gradle.daemon=true
org.gradle.caching=true
com.company.artifactory.username=drew.stephens@company.com
com.company.artifactory.apiKey=<api_key>
com.company.artifactory.contextUrl=https://company.jfrog.io/company

And I would like to move those Artifactory values to a company-specific init script, ~/.gradle/company.init.gradle (mainly because I commit my gradle.properties to my home directory repo):

gradle.allprojects {
    com.company.artifactory.username=drew.stephens@company.com
    com.company.artifactory.apiKey=<api_key>
    com.company.artifactory.contextUrl=https://company.jfrog.io/company
}

But neither of those methods makes the property accessible from my buildscript:

task('foobar') {
    // Outputs "null"
    println(project.properties['com.company.artifactory.username'])
}
2 Answers

According to gradle.allprojects your configuration action occurs against all currently available and evaluated projects, and later after new projects have been evaluated. According to how I understand what you want to achieve, I believe that gradle.beforeProject would be the place to add your properties.

Related