Extra properties getting lost (or: I'm doing it wrong)


(Alastair Drong) #1

I’ve been reading and playing around with this for a few days with not much success. I suspect this isn’t difficult, it’s just that I’m new to gradle and simply don’t know how to do this correctly.

I’ve got a directory structure filled with config files. Names of these files become my project names (which I’m doing successfully) and the path that these files are in mirror the path where my source files should live (which I’ve done successfully by hard-coding the paths). My issue is storing the path in a way that I can later parse in my build.gradle.

My directory structure/file names look something like this. Note that I expect to have duplicate file names in unique paths.

/confs --/region1 ----/qa ------projectName1.conf ------projectName2.conf ----/prod ------projectName1.conf ------projectName2.conf ----/misc ------projectPlaying.conf --/region2 ----/qa ------projectName2.conf ------projectName3.conf ----/prod ------projectName2.conf ------projectName3.conf

in my settings.gradle, I traverse the project directory.

rootDir.traverse(

type: FILES,

nameFilter: ~/.+.conf$/) { file ->

include file.name.substring(0, file.name.length() - “.conf”.length())}

I thought I could simply say ext.confFilePath=file.name in the settings file and then read it back in the build file with ext.confFilePath again, but everything I’ve tried has resulted in a “could not find property” error. I’ve also tried looking in my project, the parent project and the root project to no avail. I can only surmise that either I’m missing something basic, or these values are getting eaten by a plugin or something else.

Naturally, I’ve gotten this to work when I traverse the directory in the build file, but when I do this, it traverses the directory for every project and it breaks when I hit on two files with the same name.


(Alastair Drong) #2

From settings.gradle org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@1e2bda7

from build.gradle org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@106870c

So clearly each has it’s own extra properties and that I’ve misunderstood the point of settings.gradle. Where/when should I be storing this path information?


(Peter Niederwieser) #3

Each ‘ExtensionAware’ object (task, project, configuration, etc.) has its own extra properties. It’s important to omit ‘ext.’ when reading extra properties. Otherwise, parent scopes (which include parent projects) won’t be searched.

The settings script is evaluated in an earlier phase than build scripts (initialization vs. configuration phase). To pass information from the settings script to build scripts, you can, for example, use ‘gradle.ext.foo = “bar”’ (read as ‘gradle.foo’) or ‘gradle.rootProject { ext.foo = “bar” }’ (read as ‘foo’).


(Alastair Drong) #4

In all my searching, I failed to find an example that lead “ext.foo” with “gradle”. Hopefully I’m done banging my head against walls.

Thanks for the help.


(Alastair Drong) #5

The irony? I realized I needed to store all my path information in the project name…