Gradle
  1. Gradle
  2. GRADLE-1826

hasProperty() does not work in allprojects and subprojects closures with command line or explicitly set earlier

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Resolution: Unresolved
    • Affects Version/s: 1.0-milestone-3
    • Fix Version/s: None

      Description

      When setting a project property hasProperty() always returns false within an allprojects/subprojects block. getProperty() sees it just fine. Where is hasProperty() being delegated to? Explicitly calling it.hasProperty() works, but doesn't seem like it should be necessary.

      if (hasProperty('testProp'))

      { println 'The project sees it' }

      allprojects {
      // NOTE: Where is this going?
      if (hasProperty('testProp'))

      { println 'Allprojects sees it' }

      try

      { println 'Allprojects really sees it, value of: ' + property('testProp') }

      catch (MissingPropertyException mpe)

      { println 'not there' }

      }

        Activity

        Hide
        Gunnar Hillert
        added a comment -

        I am also hitting this issue using M6:

        ------------------------------------------------------------
        Gradle 1.0-milestone-6
        ------------------------------------------------------------
        
        Gradle build time: Thursday, November 17, 2011 5:54:12 AM UTC
        Groovy: 1.8.4
        Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
        Ivy: 2.2.0
        JVM: 1.6.0_29 (Apple Inc. 20.4-b02-402)
        OS: Mac OS X 10.6.8 x86_64
        

        I am executing:

            $ ./gradlew sonar -PsonarHostUrl=http://myurl:12345
        

        At the top level:

            println hasProperty('sonarHostUrl')   // prints true
        

        But in my Sonar block:

        sonar {
         println property('sonarHostUrl')    // prints the passed in url 'http://myurl:12345'
         println hasProperty('sonarHostUrl') // prints null
        ...
        }
        
        Show
        Gunnar Hillert
        added a comment - I am also hitting this issue using M6: ------------------------------------------------------------ Gradle 1.0-milestone-6 ------------------------------------------------------------ Gradle build time: Thursday, November 17, 2011 5:54:12 AM UTC Groovy: 1.8.4 Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010 Ivy: 2.2.0 JVM: 1.6.0_29 (Apple Inc. 20.4-b02-402) OS: Mac OS X 10.6.8 x86_64 I am executing: $ ./gradlew sonar -PsonarHostUrl=http: //myurl:12345 At the top level: println hasProperty('sonarHostUrl') // prints true But in my Sonar block: sonar { println property('sonarHostUrl') // prints the passed in url 'http://myurl:12345' println hasProperty('sonarHostUrl') // prints null ... }
        Hide
        Chris Beams
        added a comment -

        what happens if you qualify hasProperty with project.hasProperty?

        Show
        Chris Beams
        added a comment - what happens if you qualify hasProperty with project.hasProperty ?
        Hide
        Gunnar Hillert
        added a comment -

        Same issue:

        println(project.hasProperty('sonarHostUrl'))  //prints 'true'
        sonar {
            println(project.hasProperty('sonarHostUrl')) //prints 'false'
            ...
        }
        

        However, this works:

        rootProject.hasProperty('sonarHostUrl')
        

        Thanks for the help.

        Show
        Gunnar Hillert
        added a comment - Same issue: println(project.hasProperty('sonarHostUrl')) //prints ' true ' sonar { println(project.hasProperty('sonarHostUrl')) //prints ' false ' ... } However, this works: rootProject.hasProperty('sonarHostUrl') Thanks for the help.
        Hide
        Peter Niederwieser
        added a comment -

        Your problems stem from name collisions:

        1. org.gradle.Project.hasProperty() vs. Object.hasProperty() (the latter comes from Groovy)
        2. org.gradle.Project.getProject() vs. org.gradle.api.plugins.sonar.model.SonarModel.getProject()

        You need to make sure that you really refer to org.gradle.Project.hasProperty(), for example by wrapping the check in a helper method, or by assigning 'project' to a differently named variable and using that instead.

        Show
        Peter Niederwieser
        added a comment - Your problems stem from name collisions: 1. org.gradle.Project.hasProperty() vs. Object.hasProperty() (the latter comes from Groovy) 2. org.gradle.Project.getProject() vs. org.gradle.api.plugins.sonar.model.SonarModel.getProject() You need to make sure that you really refer to org.gradle.Project.hasProperty(), for example by wrapping the check in a helper method, or by assigning 'project' to a differently named variable and using that instead.
        Hide
        Gunnar Hillert
        added a comment -

        Thanks. Yes, I think I understand. Thus, it might not be an issue per se, but maybe you can explain that in the reference documentation (A note or warning). Being fairly new to Gradle+Groovy, I was assuming that hasProperty "just magically works".

        Show
        Gunnar Hillert
        added a comment - Thanks. Yes, I think I understand. Thus, it might not be an issue per se, but maybe you can explain that in the reference documentation (A note or warning). Being fairly new to Gradle+Groovy, I was assuming that hasProperty "just magically works".
        Hide
        Andrew Spina
        added a comment - - edited

        I've run into this problem as well and would argue that the behavior is 'surprising' and unintuitive. While it may be expected and understandable to Groovy/Gradle experts, I'd suggest that for someone new to Gradle it may be a significant roadblock.

        The problem seems to come from the fact that 'property' has two different semantics depending on your context. My use case was this:

        build.gradle
        task(run, type: JavaExec) {
        	description = "Runs the map reader. Use -Ptreasuremap=<path/to/instructions> to specify input"
        	main = 'spina.MapReader'
        	classpath = sourceSets.main.runtimeClasspath
                // This doesn't work. Needs to be qualified with project.
        	if(hasProperty("treasuremap")){
        		args = [treasuremap]
        	}
        }
        

        This doesn't work, but section 14.2.1 in This and That (http://gradle.org/docs/current/userguide/tutorial_this_and_that.html) implies that it should work. This seems like a direct contradiction. It may be a large change, but it might be worth using a synonym of property instead of property (perhaps attribute?) to refer to 'injected data'.

        Show
        Andrew Spina
        added a comment - - edited I've run into this problem as well and would argue that the behavior is 'surprising' and unintuitive. While it may be expected and understandable to Groovy/Gradle experts, I'd suggest that for someone new to Gradle it may be a significant roadblock. The problem seems to come from the fact that 'property' has two different semantics depending on your context. My use case was this: build.gradle task(run, type: JavaExec) { description = "Runs the map reader. Use -Ptreasuremap=<path/to/instructions> to specify input" main = 'spina.MapReader' classpath = sourceSets.main.runtimeClasspath // This doesn't work. Needs to be qualified with project. if (hasProperty( "treasuremap" )){ args = [treasuremap] } } This doesn't work, but section 14.2.1 in This and That ( http://gradle.org/docs/current/userguide/tutorial_this_and_that.html ) implies that it should work. This seems like a direct contradiction. It may be a large change, but it might be worth using a synonym of property instead of property (perhaps attribute?) to refer to 'injected data'.
        Hide
        kster
        added a comment -

        Using gradle 1.3 I encountered this problem.

        Updating the code to project.hasProperty('prop') worked for me.

        I would suggest that the documentation be updated to reflect this.

        Show
        kster
        added a comment - Using gradle 1.3 I encountered this problem. Updating the code to project.hasProperty('prop') worked for me. I would suggest that the documentation be updated to reflect this.
        Hide
        Jörg Schreiner
        added a comment -

        I encountered this problem with Gradle 1.5. Using hasProperty('prop') did not work. After changing to project.hasProperty('prop') it worked. There is nothing in the manual (14.2.1. Checking for project properties) that warns you of that trap. I naturally expected that hasProperty('prop') and property('prop') could be used in the same way ("if (hasProperty('prop') && property('prop') == 'foo')

        { ... }

        // not working!".

        I suggest you add a warning in the documentation so that the usage of hasProperty() is clarified.

        Show
        Jörg Schreiner
        added a comment - I encountered this problem with Gradle 1.5. Using hasProperty('prop') did not work. After changing to project.hasProperty('prop') it worked. There is nothing in the manual (14.2.1. Checking for project properties) that warns you of that trap. I naturally expected that hasProperty('prop') and property('prop') could be used in the same way ("if (hasProperty('prop') && property('prop') == 'foo') { ... } // not working!". I suggest you add a warning in the documentation so that the usage of hasProperty() is clarified.

          People

          • Assignee:
            Unassigned
            Reporter:
            Spencer Allain
          • Votes:
            7 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

            • Created:
              Updated: