Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking that Gradle system properties have been set

Tags:

gradle

The Question

How do I check to see if a System property has been properly set from within a build.gradle file?

The Situation

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.")
}

Some Code

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

like image 971
slifty Avatar asked Jan 03 '23 11:01

slifty


1 Answers

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.")
}
like image 126
UnlikePluto Avatar answered Feb 12 '23 19:02

UnlikePluto