为依赖关系分级多项目递归任务



配置:

我有像那样的多项目限制

project1:
implementation(1st_lvl_module1)
implementation(1st_lvl_module2)
project2:
implementation(1st_lvl_module1)
implementation(1st_lvl_module2)
implementation(1st_lvl_module3)
project3:
implementation(1st_lvl_module2)
1st_lvl_module1:
implementation(2nd_lvl_module1)
implementation(2nd_lvl_module2)
1st_lvl_module2:
implementation(2nd_lvl_module2)
1st_lvl_module3:
implementation(2nd_lvl_module2)
implementation(2nd_lvl_module3)
2nd_lvl_module1
2nd_lvl_module2
2nd_lvl_module3

问题:

我想为所有项目执行一些任务(例如gradle test(。它可以根据顶级呼叫的要求工作。但我想为每个项目独立执行它,这里我有一个问题。

如果我确实调用了gradle project1:test,它将仅针对project1执行,而不包括1st_lvl_module1,后者也使用2nd_lvl_module2实现了2nd_lvl_module1 and 2nd_lvl_module21st_lvl_module2


  • 我尝试实现自定义任务E.g
tasks.register("testWithDependencies") { task ->
task.dependsOn("test")
configurations.forEach {
it.dependencies.findAll { it instanceof ProjectDependency }.forEach {
dependsOn ":${it.name}:test"
}
}
}

通过这种方式,它也适用于一级实现。gradle project1:testWithDependencies将执行project11st_lvl_module11st_lvl_module2test任务,但仍然忽略2nd_lvl_module1 2nd_lvl_module2

  • 如果我执行此任务例如
tasks.register("pd") { task ->
configurations.forEach {
println("Config name: ${it.name}")
it.dependencies.findAll { it instanceof ProjectDependency }.forEach {
def depProject = ((ProjectDependency)it).getDependencyProject()
println("${depProject.name}")
depProject.configurations.forEach {
println("---Config name: ${it.name}")
}
}
}
}

我的project1包含implementation配置,但所有1st_lvl_module*都不包含。实际上,子模块的configurations列表看起来很差。


问题:

是否有人对多模块子项目结构有同样的问题?或者递归调用最简单的方法是存在?

解决方案:文件,例如testWithDependency.gradle,内容如下:

tasks.register("testWithDependencies") { task ->
task.dependsOn("test")
configurations.forEach {
it.dependencies.findAll { it instanceof ProjectDependency }.forEach {
dependsOn ":${it.name}:testWithDependencies"
}
}
}

应在每个项目和模块的gradle.build的最后(将验证项目的依赖性时(应用

此解决方案还可以针对多个任务进行扩展。