# onlyIf

abstract fun onlyIf(onlyIfClosure: Closure<out Any>)(source)

Execute the task only if the given closure returns true. The closure will be evaluated at task execution time, not during configuration. The closure will be passed a single parameter, this task. If the closure returns false, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage:`myTask.onlyIf { isProductionEnvironment() }`

#### Parameters

onlyIfClosure

code to execute to determine if task should be run

abstract fun onlyIf(onlyIfSpec: Spec<in Task>)(source)

Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during configuration. If the Spec is not satisfied, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage (from Java):

    myTask.onlyIf(new Spec<Task>() {
       boolean isSatisfiedBy(Task task) {
          return isProductionEnvironment();
       }
    });

Content copied to clipboard

#### Parameters

onlyIfSpec

specifies if a task should be run

@Incubating
abstract fun onlyIf(onlyIfReason: String, onlyIfSpec: Spec<in Task>)(source)

Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during configuration. If the Spec is not satisfied, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage (from Java):

    myTask.onlyIf("run only in production environment", new Spec<Task>() {
       boolean isSatisfiedBy(Task task) {
          return isProductionEnvironment();
       }
    });

Content copied to clipboard

#### Since

7.6

#### Parameters

onlyIfReason

specifies the reason for a task to run, which is used for logging

onlyIfSpec

specifies if a task should be run