Property and hasProperty do not seem to be consistent with each other

(Etienne Studer) #1

As part of the my Gradle build running on TeamCity, the following code is executed:

allprojects {
      println property('teamcity')
    println hasProperty('teamcity')

Interestingly, the first line returns a map with many values, and the second line returns false. That seems inconsistent to me.

(Peter Niederwieser) #2

Which Gradle version? Can you try:

allprojects { prj ->
    println prj.hasProperty('teamcity')

The implementations of the two methods look consistent to me.

(spencer_allain) #3

Although I no longer see a link from to the JIRA issues tracker, there is a thread where Peter explains in more detail exactly why they differ without explicitly binding a named variable to the project object.


(Etienne Studer) #4

Hi Peter

It works properly when using your suggested code.

It seems things behave differently between using an explicit project closure parameter and none.

Regards, Etienne

(Peter Niederwieser) #5

It’s likely a conflict between ‘Project.hasProperty()’ and ‘Object.hasProperty()’. The latter is added by Groovy. We should do some research whether this is something we can fix on our side.

(Etienne Studer) #6

Got it. It seems that it is safer to always use an explicit closure parameter.

(Peter Niederwieser) #7

Safer yes, but it’s a rare issue and I haven’t seen it for anything other than ‘hasProperty’. Hence I almost never use an explicit parameter.