Sometimes, we want tasks to be excluded from a build. In certain circumstances, we just want to skip a task and continue executing other tasks. We can use several methods to skip tasks in Gradle.
Every task has an onlyIf
method that accepts a closure as an argument. The result of the closure must be true
or false.
If the task must be skipped, the result of the closure must be false
, otherwise the task is executed. The task
object is passed as a parameter to the closure. Gradle evaluates the closure just before the task is executed.
The following build file will skip the longrunning
task, if the file is executed during weekdays, but will execute it during the weekend:
import static java.util.Calendar.* task longrunning { // Only run this task if the // closure returns true. onlyIf { task -> def now = Calendar.instance def weekDay = now[DAY_OF_WEEK] def weekDayInWeekend = weekDay in [SATURDAY, SUNDAY] ...