Gradle:在taskGraph.whenReady中设置变量

Rob*_*ert 3 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

...
: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
Run Code Online (Sandbox Code Playgroud)

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

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

Pet*_*ser 6

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