gradlebuild.gradlegradle.properties

Load common properties in build.gradle doesn't work for buildscript


I have multiple projects which share common properties. I want to manage common dependencies in one place so I decided to use this approach (from Edit section).

//properties.gradle
ext {
    pluginVersion = "x.x.x"
}
//build.gradle
apply from: '/path/to/properties.gradle'

buildscript {
    dependencies {
        classpath "some.group:some-plugin:${pluginVersion}"
    }
}
...

This results with following error:

Could not get unknown property 'pluginVersion'

What should I do to load properties from properties.gradle file? What am I doing wrong? Is buildscript section somehow different than other sections?

I'll just add that before extracting common properties I kept them all in gradle.properties file and they worked fine.


Solution

  • Looks like buildscript section runs before apply ...

    Try moving apply inside buildscript like that:

    //build.gradle
    buildscript {
        apply from: '/path/to/properties.gradle'
        dependencies {
            classpath "some.group:some-plugin:${pluginVersion}"
        }
    }
    ...