Skipping tasks
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.
Using onlyIf predicates
Every task has a method onlyIf
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 task longrunning
, if the file is executed during weekdays, but will execute it during the weekend:
import static java.util.Calendar.* task longrunning { onlyIf { task -> def now = Calendar.instance def weekDay = now[DAY_OF_WEEK] def weekDayInWeekend = weekDay in [SATURDAY, SUNDAY] return weekDayInWeekend } doLast { println "Do long running...