This message was deleted.
# community-support
s
This message was deleted.
g
Or let me ask another way: is there a way to execute a given task on all dependants? (not buildDependants, but more something like
myCustomTask
on all Dependants)
d
Are you looking for something like Project a has a dependency on project b, you want myCustomTask run on both projects?
g
Yes, but I don't want to run this task on say Project C, not related to these 2 connected projects
I'm able to do all this manually by looping on each subproject, their configurations and their dependencies, and assemble another graph, but it's not clean and it's very complex to cover all cases...
d
We used to do something like this a long while back on our Android project, but removed it. Our use case was: We have project A and we wanted a single command to run lint and unit test and any upstream module. The code below is how we resolved the upstream and got a certain task to pull into the command. It is rather old as we altered our CI process so lint + unit test is simply.
./gradlew lintDebug testDebugUnitTest --parallel --continue
so we can run each module in. parallel and couldn’t find a way to do it with our single command. This code may not compile/work properly with current versions as this is rather old code. (GlobalConfig was a simple utility we wrote to look at the project and. return the build variant in the format needed)
Copy code
class ModuleDependencyResolver {
  static def getDependentModulesTasks(Project project, String variant, String prefix) {
    ArrayList<String> tasks = new ArrayList<>()

    project.configurations.each { configuration ->
      configuration.getAllDependencies().each { upstreamDep ->
        if (upstreamDep.hasProperty('dependencyProject')) {
          Project depProject = project.rootProject.findProject(":${upstreamDep.dependencyProject.getName()}")
          project.evaluationDependsOn(depProject.path)

          if (depProject.hasProperty('android')) {
            String requiredVariant = GlobalConfig.getFormattedBuildVariants(depProject).find { it ->
              return variant.toLowerCase().contains(it.toLowerCase())
            }

            requiredVariant = requiredVariant == null ? depProject.android.defaultPublishConfig.capitalize() : requiredVariant
            tasks.add(depProject.getTasksByName("${prefix}${requiredVariant.capitalize()}WithDeps", false))
          }
        }
      }
    }
    return tasks
  }
}
The above groovy code would get invoked in this sort of groovy script:
Copy code
private static def configureUnitTestTask(Project project, String formattedBuildVariant) {
    project.task("unitTest${formattedBuildVariant}WithDeps", type: DefaultTask, dependsOn: "test${formattedBuildVariant}UnitTest") {
      group "Test"
      description "Runs Spec for this module and all its upstream deps"
      ModuleDependencyResolver.getDependentModulesTasks(it.project, formattedBuildVariant, 'unitTest').each { task ->
        dependsOn task
      }
      ModuleDependencyResolver.getDependentModulesTasks(it.project, formattedBuildVariant, 'lint').each { task ->
        dependsOn task
        mustRunAfter task
      }
      project.getTasksByName("check${formattedBuildVariant}Coverage", false).each { task ->
        dependsOn "lint${formattedBuildVariant}"
        finalizedBy task
      }
    }
  }