Project properties - more terse way to handle this?


(Phil Swenson) #1

we want to check for project properties, we tried this:

def nodeNumber = project.property('node') ?: 1

This throws an error: Could not find property ‘node’ on project

How can I check for a property that doesn’t throw an error? I could do a

def nodeNumber = project.hasProperty('node") ? project.property['node'] : '1'

this works, but it’s kind of long winded, not DRY.

any better approach?

thanks


(Luke Daley) #2

You can use ‘project.properties.node ?: 1’.

One thing to bare in mind is that ‘project.properties’ can be a little expensive to calculate (but rarely significantly) so if you’re doing this a lot then save ‘project.properties’ to a temp var. Each time you call ‘project.properties’ it has to recalculate as more properties may have been added.


(Phil Swenson) #3

we can’t do that approach because of this side-effect:

http://gsfn.us/t/3x4g0

sounds like what i pasted is the best way for now…?


(Luke Daley) #4

Yeah, that’s the best way because of that bug.