Gradle:在taskGraph.whenReady中设置变量

时间:2014-08-20 17:03:43

标签: gradle

我的构建脚本有以下代码:

def includePatchFrom = "WTF?!"

task patchWebXml(type: Exec) {
    executable "perl"
    args "scripts/patch.pl", includePatchFrom
}

gradle.taskGraph.whenReady { taskGraph ->
    if (taskGraph.hasTask(webtestWar)) {
        includePatchFrom = "resources/webtest"
    }
    else {
        includePatchFrom = "resources/production"
    }
}

如果我正确理解http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html,我应该能够在includePatchFrom闭包中设置whenReady变量,但它只保留其初始值:

...
:patchWebXml
...
Starting process 'command 'perl''. Working directory: /Users/robert/ Command: perl scripts/patch.pl WTF?!
Successfully started process 'command 'perl''
Cannot read WTF?!: No such file or directory at scripts/patch.pl line 43, <$F> line 14.
:patchWebXml FAILED

从println语句中我可以看出includePathFrom被设置为正确的值。似乎exec任务已经使用了includePatchFrom的旧值,并且在whenReady闭包运行时不受影响。

我在这里缺少什么,如何使用不同的补丁文件,具体取决于这是生产版还是测试版?

1 个答案:

答案 0 :(得分:6)

taskGraph.whenReady发生的时间远晚于任务的配置。到那时,改变变量的值为时已晚。相反,您必须直接(重新)配置任务(patchWebXml.args ...)。