Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run shell script from Gradle and wait for it to finish

I'm running a shell script from gradle, the issue is that the shell script is running some prerequisites that I need to be run before gradle continues.

I tried the following but seems like gradle is opening another child process for the shell script

sleep.sh
echo 'hi1'
sleep 1
echo 'hi2'
sleep 10
echo 'bye'


Gradle:
task hello3(type: Exec) {
println 'start gradle....'
    commandLine 'sh','sleep.sh'
println 'end gradle....'
}



Result:
start gradle....
end gradle....
:hello3
hi1
hi2
bye
like image 599
15412s Avatar asked Aug 25 '15 08:08

15412s


People also ask

How do I run a script in gradle?

Running Gradle Commands To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

What is doLast in gradle?

The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build. Both of these print the line, but the first one only prints the line when the testTask is supposed to be executed.

What happens when we run gradle build?

When you run the gradle command, it looks for a build file in the current directory. You can use the –b option to select a particular build file along with absolute path. The following example selects a project hello from myproject. gradle file, which is located in the subdir/.


1 Answers

Your problem is your the println statements are executed when Gradle parses the build.gradle file, not when it executes the task.

You should move your println statements to doFirst and doLast as follows to make things clear:

task hello3(type: Exec) {
  doFirst {
    println 'start gradle....'
  }
  commandLine 'sh','sleep.sh'
  doLast {
    println 'end gradle....'
  }
}

I believe, Gradle actually waits for the script to finish before doing anything else, so you do not need to do anything special to make it wait.

Gradle will always start your shell script in a child process.

like image 110
Andrey Vetlugin Avatar answered Nov 16 '22 04:11

Andrey Vetlugin