Gradle: set variable in taskGraph.whenReady

My build script has this code:

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"
    }
}

      

If I understand correctly http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html , I have to set this variable includePatchFrom

in the closure whenReady

, but it just keeps its initial value:

...
: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

      

In the println outputs, I can tell I am includePathFrom

getting the correct value. It looks like the exec task has already used the old value includePatchFrom

and is not affected when the close completes whenReady

.

What am I missing here and how can I use a different patch file depending on whether it is a production or test build?

+3


source to share


1 answer


taskGraph.whenReady

happens much later than task configuration. By then it is too late to change the value of the variable. Instead, you will have to (re) configure the task directly ( patchWebXml.args ...

).



+6


source







All Articles