简体   繁体   中英

How to call a Gradle task in all subprojects?

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 .

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")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM