Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run gradle build(test) in subprojects in a custom task

How to run task build or test some subprojects in custom task in build.gradle.kts? For example, I have submodules: firstA, secondA, thirdA. I want to run build in custom task for only firstA and thirdA.

settings.gradle.kts

rootProject.name = "myProject"
include(
  "modules:firstA",
  "modules:secondA",
  "modules:thirdA"
)
like image 259
Darkin Rall Avatar asked Nov 13 '20 07:11

Darkin Rall


People also ask

How do I run a custom Gradle task?

Go to Run -> Edit Configurations -> Click on + to add a new configuration -> Select Gradle from the list that comes up. Finally select the app, and type in the task that you want to run. Android Studio will even provide autocomplete for the same.


1 Answers

Register a gradle task in the root project, then iterate over the subprojects and filter them by their name, and then depending the task on the test tasks of the subprojects.

Example:

tasks.register("mytest") {
    dependsOn(subprojects.mapNotNull {
        when (it.name) {
            "firstA", "thirdA" -> it.tasks.findByName("test")
            else -> null
        }
    })
}
like image 160
Deadbeef Avatar answered Nov 15 '22 09:11

Deadbeef