Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping dependency execution of a disabled task in Gradle?

Is it somehow possible to not execute the dependencies of a task when that task will be skipped?

In the example below, I'd like jar (and the dependencies of jar) to not be executed if a server is already running when executing runServerTests. The server would in this case be started by another process.

apply plugin: 'java'

task startServerIfNotRunning(dependsOn: jar) {
  onlyIf { isServerNotRunning() }
  ...
}

task runServerTests(dependsOn: startServerIfNotRunning) { ... }

I'd rather not add an onlyIf to the jar task, since other tasks that always should be executed may be depending on that one. The jar task also has dependencies of its own.

like image 331
David Pärsson Avatar asked Apr 25 '13 12:04

David Pärsson


2 Answers

To get the desired behavior, you have to exclude the task from the task execution graph, rather than skipping its execution. You can do this with -x from the command line or programmatically with gradle.startParameter.excludedTaskNames << "..." or gradle.taskGraph.useFilter { task -> ... }.

like image 84
Peter Niederwieser Avatar answered Sep 21 '22 06:09

Peter Niederwieser


You can do something like

task startServerIfNotRunning(dependsOn: jar) {
    if (isServerNotRunning()) {
        enabled = false;
        dependsOn = [];
    }
}

The if statement us evaluate in configuration phase and the dependent tasks are removed. I have summarized this in Skipping Gradle Tasks with code and output. Take a look.

like image 33
Aniket Thakur Avatar answered Sep 19 '22 06:09

Aniket Thakur