skip a task in gradle, but only if the task exists

Viewed 2018

I have a complex multi sub-project gradle project with a little kotlin multiplatform inside and a fex custom gradle plugin.

My issue is , when I want to run the build by skipping some tasks(mostly test), not all the project have the same test task name : for some it's call jsTest, for other nodeJsTest, for other jvmTest.

So when I call gradle build -x jsTest -x nodeJsTest I have error because sometime some of the tasks to skip don't exist.

How can I skip the task, and ignore-it if it don't exist?

2 Answers

You can modify your Gradle file to do something like (Kotlin DSL):

tasks.named("build") {
    dependsOn.removeIf { it.toString().contains("flakyTest") }
}

Otherwise you will need to aggregate your tasks to what you want specifically either by doing ./gradlew myTask anotherTask anotherOne andAnotherOne or create a task that dependsOn all the tasks you want.

Instead of making your test tasks to depend on build directly, you can create generic test task task testType in the middle- build triggers task testType and then it triggers jsTest or whatever relevant test task you create in that module.
Now you can safely run gradle build -x testType.

For example (in your .gradle file):

task jsTest { ... }
task testType { dependsOn jsTest }
build.finalizedBy(testType)

Do the same for the rest of your test tasks files, you can also create task testType globaly if you want the solution to be cleaner.

Related