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
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.
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.
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/.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With