Skip to content

Instantly share code, notes, and snippets.

@stefan-huettemann
Created September 26, 2019 14:26
Show Gist options
  • Save stefan-huettemann/0ca41653761e548e1dcb79b596a05cfe to your computer and use it in GitHub Desktop.
Save stefan-huettemann/0ca41653761e548e1dcb79b596a05cfe to your computer and use it in GitHub Desktop.
Some tasks to print properties on cmd line in gradle buildscripts
/**
* Print value of property provided in "-Pprop" on command line to stdout.
*
* Usage Example: ./gradlew -q printProperty -Pprop=rootProject.name
*/
task printProperty {
doLast {
// get the "property name" in question from commandline:
String prop = project.findProperty('prop')
if (prop == null) {
return // or use println ...
}
// treat as property name:
Object theProperty = project.findProperty(prop)
if (null == theProperty) {
// try to handle provided information as "nested property"
List<String> thePropPath = prop.split('\\.').toList()
theProperty = project.findProperty(thePropPath.head())
// aux. closure to travel "property path"
def pp = { s, t ->
if (s != null && s.hasProperty(t).is(true)) {
return s.property(t)
}
return null
}
thePropPath.tail().each { item ->
theProperty = pp(theProperty, item)
}
}
println(theProperty ?: "") // or print "null" ...
}
}
/**
* Prints value of property "rootProject.name" to stdout.
*
* Usage: ./gradlew -q printRootProjectName
*/
task printRootProjectName {
doLast {
println(project.findProperty('rootProject').name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment