Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run gradle task and save its return value to shell script variable

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"
like image 735
Pavan Avatar asked Mar 01 '17 05:03

Pavan


1 Answers

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.

like image 81
Entreco Avatar answered Oct 18 '22 08:10

Entreco