我有一个像这样的扁平多项目配置:
projA
projB
projC
build_scripts
build_scripts
包含 build.gradle
build.gradle
看起来像这样:
allprojects {
...
task gitPull(type: Exec) {
description 'Pulls git.'
commandLine "git", "pull"
}
}
project(':projA') {
...
}
project(':projB') {
...
}
project(':projC') {
...
}
当我从 build_scripts
目录运行 gradle gitPull
时,我想要
gradle 在 projA
、projB
和 projC
目录(而不是其他地方)执行 git pull
。
但是,由于 gralde 将 build_scripts
视为根项目,因此它也会在那里执行 git pull
- 这会失败,因为它不是 git 存储库(或者即使它是是的,我不想在构建项目时对其执行 git pull )
一般来说,我不希望 build_scripts
文件夹(即根项目)参与任何与构建相关的事件。
如何排除根项目由 allProjects
中指定的任务执行?
最佳答案
来自Gradle turorials :
在此示例中,您可以看到 configure
如何从执行任务 hello
中过滤掉项目 tropicalFish
。
Example 56.8. Adding custom behaviour to some projects (filtered by project name)
....
Note: The code for this example can be found at samples/userguide/multiproject/addTropical/water in the ‘-all’ distribution of Gradle.
设置.gradle
include 'bluewhale', 'krill','tropicalFish'
构建.gradle
allprojects {
task hello << {task -> println "I'm $task.project.name" }
}
subprojects {
hello << {println "- I depend on water"}
}
configure(subprojects.findAll {it.name != 'tropicalFish'}) {
hello << {println '- I love to spend time in the arctic waters.'}
}
关于Gradle:防止执行根项目的 allProjects 中定义的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30679958/