You can use tasks.withType(AbstractCompile) , which returns all compiled tasks for all source sets (including Java, Groovy, Scala). You can then filter this out by excluding all tasks with test in them, as suggested in another answer.
For a specific task that depends on all of this, you can do the following:
myTask.dependsOn tasks.withType(AbstractCompile).matching { !it.name.toLowerCase().contains("test") }
source share