Search code examples
kotlingradlebuild.gradlegradle-kotlin-dsl

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

Solution

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