[英]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:说,我有一个 Gradle 项目的层次结构,其中一些应用了java
插件:
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
.我想在存在此任务的所有子项目中执行test
任务: :projA:projA2:test
、 :projB:projB1:test
和: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:另一种选择是遍历子项目并检查java
插件是否应用在那里:
// 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.编辑 1 :抱歉,但我忘记了一个重要细节 - 我需要在项目的子树中运行测试。 Say, everything down the path :projB
.说,一切都在路上: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
块中执行此操作。
subprojects {
myTask.dependsOn("test")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.