Gradle: running more than one command in an Exec task

A Gradle Exec task will only run the last ‘commandLine’ defined inside its block.  Putting multiple entries inside its block will not run multiple commands.

As an example, if you run the following Gradle task.

task willOnlyRunLast(type: Exec) {
  commandLine "echo", "first"
  commandLine "echo", "second"
  commandLine "echo", "last"
}

The task above will only echo “last” because it is the last ‘commandLine’ definition found.  However, you can use the ‘doLast’ block to list a series of ‘exec’ each with their own commandLine as a workaround.

task runMultiple(type: Exec) {
  commandLine "echo", "first"
  doLast {
    exec {
      commandLine "echo", "second"
    }
    exec {
      commandLine "echo", "last"
    }
  }
}

Running this task will execute all the commands provided.

REFERENCES

gradle download release

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.htmlgradle exec task