Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 834
Marcin Zajączkowski Avatar asked Dec 01 '14 23:12

Marcin Zajączkowski


People also ask

How do you define Gradle dependencies?

The following code snippet is to define the external dependency. Use this code in build. gradle file. dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' } An external dependency is declaring the external dependencies and the shortcut form looks like "group: name: version".

How do I list all tasks in Gradle?

To get an overview of all Gradle tasks in our project, we need to run the tasks task. Since Gradle 5.1, we can use the --group option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.

Does Gradle build run all tasks?

You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

How do I set default Gradle tasks?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.


2 Answers

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")
}
like image 84
Invisible Arrow Avatar answered Oct 09 '22 08:10

Invisible Arrow


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.

like image 1
Peter Niederwieser Avatar answered Oct 09 '22 08:10

Peter Niederwieser