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

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.

Which Gradle version? Can you try:

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

The implementations of the two methods look consistent to me.

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.


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

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.

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

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.