How can I get a Gradle build to load it's properties from a file other than gradle.properties?


(Tim Fox) #1

I can’t see any easy way.

I have tried loading the properties using some custom code in the script:

def loadProperties(String sourceFileName) {

def config = new Properties()

def propFile = new File(sourceFileName)

if (propFile.canRead()) {

config.load(new FileInputStream(propFile))

for (Map.Entry property in config) {

project.ext[property.key] = property.value;

}

} }

But this isn’t really what I want since I want the props as proper script properties not project.ext things, and it seems setting script properties has been deprecated :((


(Luke Daley) #2

Setting a property via ‘ext.’ has the same effect as setting it the old way. The ‘ext.’ namespace is just a safer forward declaration mechanism. What’s not working if you set it this way?

(btw: if you wrap code in <code> tags in your posts the code is much easier to read)


(Tim Fox) #3

When loading properties this it doesn’t seem to make them available inside a buildScript section, e.g.:

buildscript {

repositories {

mavenLocal()

maven { url ‘https://oss.sonatype.org/content/repositories/snapshots’ }

mavenCentral()

}

dependencies {

classpath “io.vertx:vertx-core:$vertxVersion”

classpath “io.vertx:vertx-platform:$vertxVersion”

} }

In the above vertxVersion was not defined even though there was a call to loadProperties at the top of the script. Other parts of the script seemed to see the property ok.

This is all moot now since I have found a different way to do what I want, but it might be nice for Gradle to provide a top level “loadProperties (filename)” method to make all of this easier.


(Luke Daley) #4

This is a sequencing issue. The code in ‘buildscript {}’ is executed before any other code in the script, since it defines the classpath for the rest of the script. The solution would have been to move your property loading code into the ‘buildscript {}’ block. It still would have been available to the rest of the code.