How do I check to see if a System property has been properly set from within a build.gradle
file?
I have a gradle.properties
file in my root directory that looks something like this:
systemProp.user=exampleUsername
systemProp.password=examplePassword
I would like to validate the existence of these properties, something like:
if (!System.hasProperty('user')) {
throw new InvalidUserDataException("No user found in `gradle.properties`; please set one.")
}
I've tried the following:
project.hasProperty('user')
returns false
System.properties.get('user')
returns exampleUsername
System.hasProperty('user')
returns null
System.properties.get('user') == true
returns true for non-falsey
values
You can use System.properties.containsKey('your_property')
for your purpose. It returns true if a property with the provided key exists, false otherwise. An implementation of this could look like the following:
if (!System.properties.containsKey('user')) {
throw new InvalidUserDataException("No user found in `gradle.properties`; please set one.")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With