Discussion:
is there a more "groovy" way to write this gradle code?
phil swenson
2012-05-16 15:40:38 UTC
Permalink
if (project.hasProperty("profilingEnabled") && project.profilingEnabled) {
runAEJvmArguments << "-agentlib:yjpagent"
}

seems a bit verbose to me….
Luke Daley
2012-05-16 15:51:39 UTC
Permalink
Post by phil swenson
if (project.hasProperty("profilingEnabled") && project.profilingEnabled) {
runAEJvmArguments << "-agentlib:yjpagent"
}
seems a bit verbose to me….
if (project.properties.profilingEnabled) {
runAEJvmArguments << "-agentlib:yjpagent"
}
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
phil swenson
2012-05-16 17:01:40 UTC
Permalink
Dunno why I thought I needed "hasProperty"

But if I pass in -PprofilingEnabled=false, groovy would evaluate
project.profilingEnabled as true because the property is a string set
to "false"

In other words, in a conditional groovy evaluates "false" as true.

groovy> value = "false"
groovy> if (value){
groovy> println "true"
groovy> }
true

I think I need to do this:
if (project.properties.profilingEnabled == "true"){
runAEJvmArguments << "-agentlib:yjpagent"
}
Post by Luke Daley
Post by phil swenson
if (project.hasProperty("profilingEnabled") && project.profilingEnabled) {
       runAEJvmArguments << "-agentlib:yjpagent"
}
seems a bit verbose to me….
if (project.properties.profilingEnabled) {
      runAEJvmArguments << "-agentlib:yjpagent"
}
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
---------------------------------------------------------------------
   http://xircles.codehaus.org/manage_email
Robert Fischer
2012-05-16 17:10:31 UTC
Permalink
project.properties.profilingEnabled as Boolean

~~ Robert.
Post by phil swenson
Dunno why I thought I needed "hasProperty"
But if I pass in -PprofilingEnabled=false, groovy would evaluate
project.profilingEnabled as true because the property is a string set
to "false"
In other words, in a conditional groovy evaluates "false" as true.
groovy> value = "false"
groovy> if (value){
groovy>  println "true"
groovy> }
true
if (project.properties.profilingEnabled == "true"){
  runAEJvmArguments << "-agentlib:yjpagent"
}
Post by Luke Daley
Post by phil swenson
if (project.hasProperty("profilingEnabled") && project.profilingEnabled) {
       runAEJvmArguments << "-agentlib:yjpagent"
}
seems a bit verbose to me….
if (project.properties.profilingEnabled) {
      runAEJvmArguments << "-agentlib:yjpagent"
}
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
---------------------------------------------------------------------
   http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
   http://xircles.codehaus.org/manage_email
Luke Daley
2012-05-16 17:17:54 UTC
Permalink
Post by phil swenson
Dunno why I thought I needed "hasProperty"
Because doing project.profilingEnabled will throw an exception if that property hasn't been set. All of the example code uses hasProperty.
Post by phil swenson
But if I pass in -PprofilingEnabled=false, groovy would evaluate
project.profilingEnabled as true because the property is a string set
to "false"
In other words, in a conditional groovy evaluates "false" as true.
groovy> value = "false"
groovy> if (value){
groovy> println "true"
groovy> }
true
if (project.properties.profilingEnabled == "true"){
runAEJvmArguments << "-agentlib:yjpagent"
}
Correct, depending on how exact you need to be.

You could do:

if (Boolean.valueOf(project.properties.profilingEnabled)){
runAEJvmArguments << "-agentlib:yjpagent"
}
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
Robert Fischer
2012-05-16 17:26:03 UTC
Permalink
But project.properties.profilingEnabled doesn't throw an exception, right?

~~ Robert.
Post by Luke Daley
Post by phil swenson
Dunno why I thought I needed "hasProperty"
Because doing project.profilingEnabled will throw an exception if that property hasn't been set. All of the example code uses hasProperty.
Post by phil swenson
But if I pass in -PprofilingEnabled=false, groovy would evaluate
project.profilingEnabled as true because the property is a string set
to "false"
In other words, in a conditional groovy evaluates "false" as true.
groovy> value = "false"
groovy> if (value){
groovy>  println "true"
groovy> }
true
if (project.properties.profilingEnabled == "true"){
  runAEJvmArguments << "-agentlib:yjpagent"
}
Correct, depending on how exact you need to be.
if (Boolean.valueOf(project.properties.profilingEnabled)){
 runAEJvmArguments << "-agentlib:yjpagent"
}
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
---------------------------------------------------------------------
   http://xircles.codehaus.org/manage_email
Luke Daley
2012-05-16 17:31:22 UTC
Permalink
Post by Robert Fischer
But project.properties.profilingEnabled doesn't throw an exception, right?
Correct, it's using the standard Groovy feature of obtaining all of the project properties as a Map<String, ?>. We extend this in Gradle to be aware of all of the dynamic properties.
--
Luke Daley
Principal Engineer, Gradleware
http://gradleware.com
Loading...