Properties for initscript block

I created a Gradle plugin and I want to apply that plugin in an initialization script. In the docs I found the initscript method, which seems to be nothing more but an alias for the regular buildscript method, relying to the source code. Therefor, I understand that the method will always be executed first.

At some point during initialization phase, properties (project / system) are loaded, but I assume that this happens after the initscript closure has been handled. Is there any way to use properties (of any kind) in this closure?

I my case, I try to create a global initialization script for a company and I need to load a plugin inside a initialization script. But I have to use an artifactory server, which is protected by credentials. The credentials should be stored in the user home gradle.properties file. I already tried to access the project properties in every way I can imagine:

initscript {
    repositories {
        maven {
            url 'http://my.local/artifactory/repo'
            credentials.username artifactoryUser
            credentials.password artifactoryPassword
            // Even tried to access directly via startParameter.projectProperties['artifactoryUser'] (or systemPropertiesArgs)
        }
    }
}

The corresponding gradle.properties file:

artifactoryUser=myname
artifactoryPassword=12345mygeneratedpassword67890

Of course, I added the systemProp. prefix when trying to access them as system properties.

1 Like

You always have the option of manually loading the specific property file you need in your init script:

// Load
Properties properties = new Properties()
properties.load(new File(getGradleUserHomeDir(), 'gradle.properties').newDataInputStream())

// Access
properties.artifactoryUser
properties.artifactoryPassword

Yeah, I aready thought of that solution, but, to be honest, I did not really like it. Now, since it seems like a better solution does not exist, I decided to also use the Java Properties class, but to load a new file artifactory.properties to avoid problems when accessing gradle.properties.

Nevertheless, thank you very much for your efforts and the confirmation, that no better solution exists.

Ok so this works as long as the properties are loaded inside the initscript closure. What about loading properties globally for all of the init.gradle file? I tried this but it does not work:

Properties properties = new Properties()
properties.load(new File(getGradleUserHomeDir(), 'gradle.properties').newDataInputStream())
initscript {
    repositories {
        maven {
            url properties.artifactoryUrl  // Gives error
        }
    }
}

Could not get unknown property ‘properties’ for object of type org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository.