hasProperty() and gradle -P switch

If I have a task:

task test {
println hasProperty(‘foo’)
println foo;
}

and run

gradle -Pfoo=5

why does it return?

null
5

same answer for

task test << {
println hasProperty(‘foo’);
println foo;
}

I’m using gradle 3.0. What property is hasProperty referring to? I guess it’s not the Project instance but if not what?

v/r,

Mark

Answering my own question:

task test {
println project.hasProperty(‘foo’);
println foo;
}

works

I wasn’t able to get Project.findProperty() to work. It always returns the following error:

No signature of method: static org.gradle.api.Project.findProperty() is applicable for argument logic: (java.lang.String) value: [project.foo]
Possible solution: hsaProperty(java.lang.String)

How does findProperty work? Seems like Project.findProperty(‘foo’) should work.

Any help is greatly appreciated.

v/r,

Mark

The task.

EDIT: findProperty() is not a static method. I missed that part in your message. Use lowercase ‘project’.

This is working for me in Gradle 3.0, Linux, Java 8.

task bar {
    println project.findProperty( 'foo' )
    println findProperty( 'foo' )
    doLast {
        println project.findProperty('foo')
        println findProperty('foo')
    }
}

Hmm…weird. I cut-n-pasted your code into a build.gradle and ran “gradle bar”. This fails as I expected but your info was enough to figure out why.

I’m running. Gradle 3.0, Linux, Java 7 while you’re running Java 8.

I upgraded to Java 8 and this resolved the problem. So, given that Gradle requires JVM 7 or higher this appears to be a bug at least in the documentation.

Thanks for your help, Chris!

v/r,

Mark

Interesting. I don’t have Java 7 on my Linux box, but here’s the results from my Mac (I added println JavaVersion.current() for paranoia purposes).

$ JAVA_HOME=`/usr/libexec/java_home -v 1.8` ./gradlew bar -q -Pfoo=5
1.8
5
5
5
5
$ JAVA_HOME=`/usr/libexec/java_home -v 1.7` ./gradlew bar -q -Pfoo=5
1.7
5
5
5
5

What type of failure do you see on Java 7?

Just the error message I mentioned above:

No signature of method: static org.gradle.api.Project.findProperty() is applicable for argument logic: (java.lang.String) value: [project.foo]
Possible solution: hsaProperty(java.lang.String)

Shouldn’t matter but I’m on Ubuntu 14.04 LTS

From my CentOS 5 box, Java 1.7 is working:

$ JAVA_HOME=/usr/java/jdk1.8.0_60 ./gradlew bar -q -Pfoo=5
1.8
5
5
5
5
$ JAVA_HOME=~/sandbox/jdk1.7.0_79 ./gradlew bar -q -Pfoo=5
1.7
5
5
5
5

The fact that you get an error about not being able to locate a static method leads me to believe that you have a capitalized project reference somewhere, however it makes no sense that it would work in Java 8 and not 7.
The only way I’ve been able to reproduce a similar error as you is if I try calling Project.findProperty() instead of project.findProperty(), which results in:

> No signature of method: static org.gradle.api.Project.findProperty() is applicable for argument types: (java.lang.String) values: [foo]
  Possible solutions: hasProperty(java.lang.String)

Also note that your error message says that the argument value is ‘project.foo’, perhaps that helps you narrow this down. Also, does the stack trace generated when running gradle with -s help locate the point of failure?

Just wanted to give my current solution to this issue in Gradle for anyone else struggling with this problem:

ext.foo = project.hasProperty(‘foo’) ? foo : 64

foo defaults to 64 but you can update via

./gradlew -Pfoo=8 build

Thanks for your help.