Skip to content

Instantly share code, notes, and snippets.

@y-polek
Last active February 5, 2022 15:30
Show Gist options
  • Save y-polek/f5acfd982d603a78e840f06bd58993dc to your computer and use it in GitHub Desktop.
Save y-polek/f5acfd982d603a78e840f06bd58993dc to your computer and use it in GitHub Desktop.
Gradle: Read property from file
/**
* Read property with {@code propertyName} from file with {@code fileName}.
* <p>
* Example of property file content:<br>
* api_key=5c7f743fe85eae73489af35c1a387a05
* apiSecret=12345678910
*
* @return Value of specified property.
* @throws GradleException if file not found or there is no specified property in a file.
*/
def property(String fileName, String propertyName) {
def propsFile = rootProject.file(fileName)
if (propsFile.exists()) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
if (props[propertyName] != null) {
return props[propertyName]
} else {
throw new GradleException("There is no '" + propertyName + "' property in '" + propsFile.name + "' file")
}
} else {
throw new GradleException("'" + propsFile.name + "' file does not exist")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment