Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Forked from rishabhmhjn/executeShellInGradle
Created October 12, 2017 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxfactorial/12bca57331e2a4a97919f9bf98db7c16 to your computer and use it in GitHub Desktop.
Save fxfactorial/12bca57331e2a4a97919f9bf98db7c16 to your computer and use it in GitHub Desktop.
Executing shell commands and getting output in gradle
def getVersionName = { ->
def hashStdOut = new ByteArrayOutputStream()
exec {
commandLine "git", "rev-parse", "--short", "HEAD"
standardOutput = hashStdOut
}
def buildNumberStdOut = new ByteArrayOutputStream()
exec {
commandLine 'echo', "$BUILD_NUMBER"
standardOutput = buildNumberStdOut
}
return buildNumberStdOut.toString().trim() + '-' + hashStdOut.toString().trim()
}
def getVersionName2() {
return "echo $BUILD_NUMBER".execute().text.trim() +
"-" +
"git rev-parse --short HEAD".execute().text.trim()
}
task printGitVersionName {
doLast {
println getVersionName()
println getVersionName2()
}
}
// execute as "./gradlew -q printGitVersionName -PBUILD_NUMBER=457"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment