Execute scripts in order in gradle

There may be a scenario where you want to execute or call multiple shell scripts from gradle. But you also want the second one to start execution only after the first one finishes.
Normally, if you call multiple shell scripts using its commandLine option, they all run in parallel or asynchronously.

An example would be first shell script creating a file and writing some data to it and second shell script reading the file.
It makes no sense for the second shell script to start executing if the first one has not yet finished.
In this article, we will take a look at 4 different ways to execute multiple shell scripts in an order.

1. Using dependsOn
Gradle script for executing two shell scripts one after another with this method is

task execScript1(type: Exec) {
    commandLine 'sh', 'script1.sh'
}

task execScript2(type: Exec) {
    dependsOn runScript1
    commandLine 'sh', 'script2.sh'
}

The first task execScript1, will execute the first shell script script1.sh.
We have marked the second task, execScript2, to depend on the first one with dependsOn.
So, the second shell script script2.sh only after the first task has completed.

A variation of this method is

task execScript1(type: Exec) {
    commandLine 'sh', 'script1.sh'
}

task execScript2(type: Exec, dependsOn: execScript1) {
    commandLine 'sh', 'script2.sh'
}

Here, the first task, execScript1, runs the first shell script script1.sh.
The second task, execScript2, specifies a dependency on the first task using the dependsOn property.

2. Using execute and waitFor
In a Gradle build script, you can use the execute() method to run a shell script and obtain a Process object that represents the process executing the script.
You can then call the waitFor() method on that Process object to wait until the script has finished executing before moving on to the next task.
In this way, Gradle provides a mechanism for integrating shell scripts into the build process and ensuring that they are executed in the desired order.

task execScripts {
    doLast {
        def script1 = "sh script1.sh".execute()
        script1.waitFor()

        def script2 = "sh script2.sh".execute()
        script2.waitFor()
    }
}

execScripts task uses the doLast closure to run the two shell scripts script1.sh and script2.sh one after the other, with waitFor() used to wait for the completion of each script before starting the next one.

3. Using &&
Another way to run shell scripts in Gradle with one script waiting for the other to finish is to use commandLine option with && operator.
Example,

task execScripts(type: Exec) {
    commandLine 'sh', '-c', './script1.sh && ./script2.sh'
}

Above task uses the commandLine property to specify a shell command that runs both script1.sh and script2.sh one after the other.
&& operator is used to run the second script only if the first one exits successfully.
This way, script2.sh will not start until script1.sh has completed.
4. Using exec

task execScripts(type: Exec) {
    exec {
        commandLine 'sh', 'script1.sh'
    }
    exec {
        commandLine 'sh', 'script2.sh'
    }
    }

In this example, both script1.sh and script2.sh are executed using the exec method.
Both scripts are executed in the desired order, with the second script waiting for the first one to finish.

Hope the article was useful.