Project.property(name) does not take value from 'ancestor'

Either I do not correctly understand what is the ‘ancestor’ projects in terms of Gradle or project.property(name) does not work according to spec of project.property(name):

6 . Search up through this project’s ancestor projects for a convention property or extra property with the given name.

In the test project (child-l1:child-l2) below I expect this to be printed (because values are redefined in the immediate parent project child-l1):

version = '1.2.3(child-l1)'
extProp = 'extProp-ValueFromChildL1'
extProp1 = 'extProp-ValueFromChildL1'

but I’m getting following (values are taken from root gradle.properties)

version = 1.2.3(gradle.properties)
extProp = extProp-ValueFromGradleProperties
extProp1 = extProp1-ValueFromGradleProperties

Test project is attached and its files are provided below. For the test, i’m running

gradle none

in the folder ./child-l1/child-l2. Gradle version is 1.10


settings.gradle

println 'Evaluating settings'

rootProject.name = 'test'

include 'child-l1'
include 'child-l1:child-l2'

gradle.properties

#org.gradle.configureondemand=false
version=1.2.3(gradle.properties)
extProp=extProp-ValueFromGradleProperties
extProp1=extProp1-ValueFromGradleProperties

build.gradle

println 'Evaluating root'

project.version='1.2.3(root)'
project.ext.extProp = 'extProp-ValueFromRoot'
project.metaClass.extProp1 = 'extProp-ValueFromRoot'

./child-l1/build.gradle

println 'Evaluating child-l1'

project.version='1.2.3(child-l1)'
project.ext.extProp = 'extProp-ValueFromChildL1'
project.metaClass.extProp1 = 'extProp-ValueFromChildL1'

./child-l1/child-l2/build.gradle

println 'Evaluating child-l2'

println 'version=' + project.version
println 'extProp=' + project.property('extProp')
println 'extProp1=' + project.property('extProp1')

println 'extProp(parent)=' + project.parent.property('extProp')

task none {}

test-project.zip (1.5 KB)

I’ve got why it is working this way - it appears that values from root ‘gradle.properties’ are fed into project.ext of every sub-module before evaluation. This way - property will be available immediately in each project, but it will not be possible to redefine value in parent for child projects.

I guess it would be better to feed ‘gradle.properties’ only into ‘rootProject.ext’. Child projects should lookup value in parents and have ability to (optionally) redefine value for self and child projects.

I’ve tried to workaround the problem by moving ‘non-global’ properties from gradle.properties into root build.gradle. Apart from being non-natural to define build properties explicitly in build.gradle, it also just does not work reliably:

  • project.version is nor inherited to children and nor evaluated from ancestors - thus always undefined
  • with org.gradle.configureondemand=true intermediate parent project is not evaluated and therefore behavior is different:

when

  • configureondemand=true - output is extProp=extProp-ValueFromRoot
  • configureondemand=false - output is extProp=extProp-ValueFromChildL1

So … it seems there is no reliable workaround for that problem.

Regarding inheritance of project version into children - it may be not applicable to most of projects, but in my case each sub-tree of sub-modules has own version and I’d like to set it once in the sub-tree root for all its children.