How do I set a project property in settings.gradle, for use in build.gradle?

I would like to set a property in the settings.gradle file, and then use that property in the build.gradle file.

I tried the following:

settings.gradle

gradle.rootProject.ext.username = "foo"
gradle.rootProject.ext.password = "bar"

build.gradle

buildscript {

    ext {
        grailsVersion = project.grailsVersion
    }
    repositories {
        mavenLocal()
        maven{
            url = "http://dc1artifctry01:8081/artifactory/releases"
            credentials {
                username project.username
                password project.password
            }
        }
    }
    dependencies {
        // ...
    }
}

But this results in the following error:

FAILURE: Build failed with an exception.

* Where:
Settings file 'C:\eclipse_ws\test_build\settings.gradle' line: 2

* What went wrong:
A problem occurred evaluating settings 'test_build'.
> The root project is not yet available for build.

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating settings 'test_build'.
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
        at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$2.run(DefaultScriptPluginFactory.java:155)
        at org.gradle.configuration.DefaultScriptTarget.addConfiguration(DefaultScriptTarget.java:69)
        at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:160)
        at org.gradle.initialization.ScriptEvaluatingSettingsProcessor.applySettingsScript(ScriptEvaluatingSettingsProcessor.java:72)
        at org.gradle.initialization.ScriptEvaluatingSettingsProcessor.process(ScriptEvaluatingSettingsProcessor.java:62)
        at org.gradle.initialization.PropertiesLoadingSettingsProcessor.process(PropertiesLoadingSettingsProcessor.java:37)
        at org.gradle.initialization.NotifyingSettingsProcessor$1.create(NotifyingSettingsProcessor.java:42)
        at org.gradle.initialization.NotifyingSettingsProcessor$1.create(NotifyingSettingsProcessor.java:39)
1 Like

I do it as follows in settings.gradle.kts (using kotlin, but a groovy version should be similar enough):

gradle.projectsLoaded({
    rootProject.extra.set("someProperty", "someValue")
})