I would like to be able to use an optional extra property in my file build.gradle
. To do that, I have to check if the property is defined but I have the following error
Cannot get property 'prop' on extra properties extension as it does not exist
File build.gradle
[...]
def my_prop=''
if(gradle.ext.prop!= null && gradle.ext.prop != '')
my_prop=gradle.ext.prop
It is working with this file setting.gradle
gradle.ext.prop='PROP'
It is not working when I commented line in file setting.gradle
//gradle.ext.prop='PROP'
How can I check in build.gradle that the gradle.ext.prop
exists ?
The ext
property is of type ExtraPropertiesExtension
: see this DSLdocumentation.
To summarize:
get(propName)
method will throw UnknownPropertyException
if the property is not set,has(propName)
method can be used to check if a property is set before trying to access itIn your case, you could use has(name)
method as follows
def my_prop = ''
if (gradle.ext.has("prop") && gradle.ext.prop != '') {
println "Property PROP found: $gradle.ext.prop"
my_prop = gradle.ext.prop
// or: my_prop = gradle.ext('prop')
} else {
println "Property PROP not set."
}