Reputation: 721
Is there a way to run a gradle task and save it output to shell variable ?
For example lets consider a gradle task that prints module version :
task getVersion << {
println '2.2.0'
}
I run this task in the shell like this :
$./gradlew getVersion
Is it possible to save output of gradle task getVersion into shell variable. For example:
VERSION=`./gradlew getVersion`
echo "Module Version is $VERSION"
Upvotes: 11
Views: 10431
Reputation: 65
If you are using the Kotlin DSL to write the task, you can do it without printing the newline. In your build.gradle.kts
:
tasks.register("getVersion") {
doLast {
print(project.version)
}
}
And then you can execute from your terminal:
VERSION=$(./gradlew -q getVersion)
Upvotes: 1
Reputation: 12900
In bash, you can do it like this:
VERSION=$(./gradlew -q getVersion | tail -n 1)
-q
: set gradle output to quit
| tail -n 1
: only use the last line of the output in your variable. Might not need this part, but sometime gradle outputs warnings/errors before printing the actual output. I personally experienced this while upgrading to gradle4.1. After the upgrade it also showed Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
Upvotes: 26
Reputation: 521
try this
exec {
commandLine "./gradlew getVersion"
standardOutput = output
}
VERSION = output.toString().trim()
Upvotes: -1