Defining dependencies between tasks
Until now, we have defined tasks independent of each other. However, in our projects, we need dependencies between tasks. For example, a task to package compiled class files is dependent on the task to compile the class files. The build system should then run the compile task first, and when the task is finished, the package task must be executed.
In Gradle, we can add task dependencies with the dependsOn
method for a task. We can specify a task name as the String
value or task
object as the argument. We can even specify more than one task name or object to specify multiple task dependencies. First, let's look at a simple task dependency:
task first << { task -> println "Run ${task.name}" } task second << { task -> println "Run ${task.name}" } // Define dependency of task second on task first second.dependsOn 'first'
Note that we define the dependency of the second
task on the first
task, in the last line. When we run...