Gradle配置任务'running'如何排序?

How to order the 'running' of configuration tasks in Gradle?

我目前有一个配置任务依赖于我从命令行调用的执行任务,即

task deployTest(dependsOn: [build, assembleForTest]) << {
    ...
}

这个任务基本上应该获取我在 assembleForTest 中组装的文件,然后部署它们(ssh 等)

我的assembleForTest代码:

task assembleForTest(type: Sync) {
    fileMode = 0775
    from ("scripts") {
        include "**/*.cgi"
        filter(org.apache.tools.ant.filters.ReplaceTokens, 
        tokens: [programName: programName, 
                 version: version, 
                 dbServer: dbServerTest,
                 deployDir: deployDirTest])
    }
    from files("scripts/" + programName + ".cgi") 
    from files("build/libs/" + programName + "-" + version + ".jar")
    into ("build/" + programName)   
}

但问题是:我的项目是在这个配置任务 assembleForTest 有 运行 之后构建的。即它将在组装完成后尝试构建,这意味着尝试进行过时的(或不存在的)部署。

Gradle 有一些我见过的最糟糕的文档,我已经使用它一段时间了,但我仍然不明白理想的设置。

这不能解决您的问题吗?

task assembleForTest(type: Sync, dependsOn: build) {
    /* configuration phase, evaluated always and before execution phase of any task */
    ...
}

task deployTest(dependsOn: assembleForTest) << {
    /* execution phase, evaluated only if the task is invoked and after configuration phase for all tasks has been finished */
    ...
}

编辑:我在示例中添加了注释。请注意,第一个任务由配置提供,而第二个任务由操作提供。切换是用左移运算符完成的。替代语法,特别有助于结合两个阶段的定义,如下所示:

task plop() {
    // some configuration
    ...
    doLast {
        // some action
        ...
    }
}

如果您将 println 替换为 'some configuration',无论调用什么任务,它都会始终打印,因为这是在配置阶段评估的。