Search code examples
javagroovygradlegradle-custom-plugin

How to depend on all *compile and *testCompile tasks in Gradle


I would like to have in animalSniffer plugin one task to depend on compilation of all production classes (Java, Groovy, Scala) in all sourceSets and the second to depend on compilation of all test classes in all sourceSets (possibly separate test and integrationTest).

I wouldn't like to depend on *classes tasks as *classes tasks should depend animalSniffer tasks (which detects Java version API incompatibilities after the compilation and can stop the build).

Is there a better way in Gradle to achieve that than checking if an instance of AbstractCompile task name starts with "compileTest"?


Solution

  • You can use tasks.withType(AbstractCompile) which returns all compile tasks for all source sets (which includes Java, Groovy, Scala). You can then filter on this by eliminating all tasks that have test in them as suggested in the other answer.

    For a specific task to depend on all these, you can do the following:

    myTask.dependsOn tasks.withType(AbstractCompile).matching {
        !it.name.toLowerCase().contains("test")
    }