Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gradle union task, execute in order

Gradle: Union wrapper task to execute subtasks in order

This is how to create a union tasks to execute subtasks in order

task A { doLast { println 'A' }}
task B { doLast { println 'B' }}
task C { doLast { println 'C' }}
task D { doLast { println 'D' }}

task unionTask {
  def containedTasks = [A, B, C, D]
  dependsOn containedTasks
  placeTasksInOrder(containedTasks)
}

def placeTasksInOrder(List tasks) {
  for (int i=0; i < tasks.size() -1; i++) {
    def earlierTask = tasks.get(i)
    def laterTask   = tasks.get(i +1)
    laterTask.mustRunAfter(earlierTask)
  }
}

dependsOn causes the other tasks to run when unionTask is run and the helper function placeTasksInOrder places the tasks in order by calling mustRunAfter

See also Stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment