Skip to content

Instantly share code, notes, and snippets.

@AndrewReitz
Created October 6, 2017 18:31
Show Gist options
  • Save AndrewReitz/3e08857f3b5cd8b1e13e98d98aab8b08 to your computer and use it in GitHub Desktop.
Save AndrewReitz/3e08857f3b5cd8b1e13e98d98aab8b08 to your computer and use it in GitHub Desktop.
Gradle Property / Configuration Loader
/**
* Grabs a configuration value from configuration property or system properties (in that order) if
* one exits. Otherwise the default value will be used. If a default value is not provided an empty
* string will be used.
*
* @param values Map containing the property name to look for (propertyName), the system environment
* variable name to look for (environmentPropertyName), and the default value if any to use
* if a value was not found.
* @return the first found value, project property, then system, then default value. If no default
* value was provided an empty string will be returned.
* @throws NullPointerException if `propertyName` or `environmentPropertyName` was not provided.
*/
ext.loadConfigValue = { Map values ->
def propertyName = values.propertyName ?: { throw new NullPointerException('propertyName == null')}()
def systemEnvironmentName = values.environmentPropertyName ?: ''
def defaultValue = values.defaultValue ?: ''
project.hasProperty(propertyName) ? project.getProperty(propertyName) : System.getenv(systemEnvironmentName) ?: defaultValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment