John Jai
John Jai

Reputation: 3733

gradle - ignore dependsOn task execution

If a condition is not met, I am trying to stop execution of a task using onlyIf(). Is there a way I can stop the task dependencies too from executing? Seems onlyIf does not stop dependencies from execution.

In the example below, I desire taskA not executed when I pass executeMe parameter as false.

build.gradle

task taskA() {
    doFirst {
        println 'executing taskA'
    }
}

task taskB(dependsOn: 'taskA') {
    onlyIf {
        executeMe.toBoolean()
    }
    doFirst {
        println 'executing taskB'
    }
}

Run output:

>gradle taskB -PexecuteMe=false
10:39:36 AM: Executing external task 'taskB -PexecuteMe=false'...
:taskA
executing taskA
:taskB SKIPPED

Upvotes: 1

Views: 2243

Answers (1)

Koby
Koby

Reputation: 615

One way you can achieve this is by adding "onlyIf" to both tasks at the same time using the following:

task taskA() {
    doFirst {
        println 'executing taskA'
    }
}

task taskB(dependsOn: 'taskA') {    
    doFirst {
        println 'executing taskB'
    }
}

[taskA, taskB].each { task ->
    task.onlyIf {
        executeMe.toBoolean()
   }    
}

Output:

$ gradle taskB -PexecuteMe=false
:taskA SKIPPED
:taskB SKIPPED

BUILD SUCCESSFUL

Total time: 2.122 secs

Upvotes: 3

Related Questions