Groovy Scripting or Gradle – check if binding variable (or Gradle property) is defined

In scripting, its helpful to create global variables.  It’s definitely not a good idea for any form of scalability, obviously, but Groovy has done a good job in creating this slight separation of function.

The difference is that the following won’t work:

but the following will work:

This can cause some problems in scripting if you need to either provide default values or if you need it to provide a better exception if the variable isn’t found.

An easy solution to this is the following:

Or if you’d like to get a null value for an optional binding:

Similarly, when using Gradle, which is based off of Groovy, you can define one or more gradle.properties files. An example gradle properties would be as simple as:

use the following to determine if a property is defined:

Or if you’d like to get a null value for an optional property: