Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: set variable in taskGraph.whenReady

Tags:

gradle

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 http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html correctly, I should be able to set that includePatchFrom variable in the whenReady closure, 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

From println statements I can tell that includePathFrom gets set to the correct value. It seems like the exec task already has used the old value of includePatchFrom and is not affected when the whenReady closure runs.

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

like image 233
Robert Avatar asked Dec 29 '25 17:12

Robert


1 Answers

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

like image 147
Peter Niederwieser Avatar answered Jan 01 '26 19:01

Peter Niederwieser