Created
September 26, 2019 14:26
-
-
Save stefan-huettemann/0ca41653761e548e1dcb79b596a05cfe to your computer and use it in GitHub Desktop.
Some tasks to print properties on cmd line in gradle buildscripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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