Gradle:防止执行根项目的 allProjects 中定义的任务

标签 gradle build build-process

我有一个像这样的扁平多项目配置:

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 在 projAprojBprojC 目录(而不是其他地方)执行 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/

相关文章:

android-studio - Libgdx:使用操作系统独立路径 'libgdx-freetype.dylib'找到了多个文件

Gradle doFirst()执行顺序

php - 使用 Subversion 制作 PHP 项目的生产版本

objective-c - 声明类

c# - 无法将目标平台更改为 "any CPU"

php - 构建脚本是否在继续之前等待命令行任务完成?

java - 无法在 gradle.properties 中设置基于 Windows 的路径

eclipse - 为什么使用 Eclipse STS 插件创建 gradle 项目需要这么长时间?

javascript - Gulp/Lazypipe : Error: Invalid call to lazypipe(). pipe():参数不是函数