7

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"?

Marcin Zajączkowski
  • 4,036
  • 1
  • 32
  • 41

2 Answers2

4

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")
}
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
Invisible Arrow
  • 4,625
  • 2
  • 23
  • 31
  • thanks for your reply, but as I mentioned in my first sentence I need to distinguish **production** and **test** compile tests to use in my two different tasks. Therefore my proposal was to in addition to `AbstractCompile` task type check also task name, but I would prefer more elegant solution. – Marcin Zajączkowski Dec 02 '14 at 09:25
  • compile tests -> compile tasks – Marcin Zajączkowski Dec 02 '14 at 09:49
  • Sorry I missed that part. Have edited my answer to filter the tasks to remove all tasks that start with "test". Hope that helps. – Invisible Arrow Dec 02 '14 at 12:21
  • Unfortunately I hoped there is a better way than mentioned in the question solution with AbstractCompile and checking the name. Anyway thanks for the answer with a code snippet - will be useful for people looking for it in the future. – Marcin Zajączkowski Dec 02 '14 at 23:05
1

If you need to differentiate between production and test compile tasks/source sets, checking whether the name contains test (case-insensitive) is the best solution that's available.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259