Skipping dependency execution of a disabled task in Gradle?

Is it possible to somehow not fulfill the dependencies of a task when this task is skipped?

In the example below, I would like the jar (and jar dependencies) not to execute if the server is already running while running runServerTests . In this case, the server will be started by another process.

 apply plugin: 'java' task startServerIfNotRunning(dependsOn: jar) { onlyIf { isServerNotRunning() } ... } task runServerTests(dependsOn: startServerIfNotRunning) { ... } 

I do not want to add onlyIf to the jar task, since other tasks that should always be performed may depend on this. The jar task also has its own dependencies.

+6
source share
2 answers

To get the desired behavior, you must exclude the task from the schedule of the task, and not skip its execution. You can do this using -x from the command line or programmatically using gradle.startParameter.excludedTaskNames << "..." or gradle.taskGraph.useFilter { task -> ... } .

+3
source

You can do something like

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

The if statement, which we evaluate at the configuration stage, and dependent tasks are deleted. I have summarized this in Skip Gradle Tasks with code and exit. Take a look.

+2
source

Source: https://habr.com/ru/post/943619/


All Articles