Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Gradle task in all subprojects?

Tags:

java

gradle

Say, I have a hierarchy of Gradle projects and some of them have java plugin applied:

root
  projA
    projA1
    projA2 (java)
  projB
    projB1 (java)
    projB2 
      projB21 (java)
      projB22 (java) 
  projC (java)

I want to execute the test task in all subprojects where this task exists: :projA:projA2:test, :projB:projB1:test and :projC:test. Probably I will add more projects in future and I don't want to manually support a list of all test tasks in all subprojects. How can I achieve it?

One thing that came to my mind is something like the following:

// In root I iterate over all subprojects and find the task by name causing
// its creation and configuration

tasks.register("testAll") {
  dependsOn subprojects.findResults { it.tasks.findByName("test") }
}

I don't like this approach as it goes against task configuration avoidance style.

Another option is to iterate over subprojects and check if the java plugin is applied there:

// In root

tasks.register("testAll") {
  dependsOn subprojects.findAll { it.plugins.hasPlugin("java") }.collect { it.tasks.named("test") }
}

It works but I have a filling that I miss something simpler...

EDIT 1: Sorry for that but I forgot one important detail - I need to run tests in a subtree of projects. Say, everything down the path :projB.

like image 821
sedovav Avatar asked Oct 18 '25 11:10

sedovav


1 Answers

Unless I'm missing something, you want to run tests for all of your submodules.

You can just...do that.

./gradlew clean test

This will run the test task in all of the subprojects that have it sufficiently configured.

If you need to run the tasks in a specific subproject, from the root project you can specify the subproject you want to run the task.

./gradlew clean :projB:test

If your subprojects have a task that needs to run after test, then you can do this in your subprojects block.

subprojects {
    myTask.dependsOn("test")
}
like image 84
Makoto Avatar answered Oct 20 '25 01:10

Makoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!