How to start a shell script from Gradle and wait for it to complete

I am running a shell script from gradle, the problem is that in the shell script some prerequisites are fulfilled, which must be fulfilled before gradle.

I tried the following, but it looks like gradle is opening another child process for a 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 
+5
source share
1 answer

Your problem is that your println instructions are executed when Gradle parses the build.gradle file, and not when it does the task.

You should move your println statements to makeFirst and doLast as follows so that everything is clear:

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

I believe Gradle is actually waiting for the script to complete before doing anything else, so you don't need to do anything special to make it wait.

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

+1
source

All Articles