Igor Artamonov
Igor Artamonov

Reputation: 35951

Gradle doesn't really honor execution order

I have a Gradle build 3 tasks, first one should set type of deployment (test/staging/production), seconds just a helper to show current type. Third one implements actual deployment (not so important for this question, actually). Like this:

task toTest() {
    project.ext.set('deployType', 'test')
    println "Set deploy type: ${project.deployType}"
}
task showStatus() {
    mustRunAfter = ['toTest', 'toStage', 'toProd']
    println "Curr deploy type: ${project.deployType}"
}
task deploy(dependsOn: [toTest, showStatus]) {
   ...
}

The problem that Gradle says that it runs in following order: toTest -> showStatus -> deploy. But as I see from console it uses different order:

////....initial tasks
:buildSrc:build UP-TO-DATE
Curr deploy type: local

Set deploy type: test
:toTest UP-TO-DATE
////.......a lot of other tasks
:showStatus UP-TO-DATE
////... final deployment

I see two opposite things: order of execution is toTest->showStatus, but output first comes from showStatus and only then from toTest. How that's possible? What i'm doing wrong?

Gradle 2.2

PS Also, seems that project.ext is just ignored, or value is not visible from other tasks. But seems that it's a topic for another question.

Upvotes: 0

Views: 50

Answers (1)

cfrick
cfrick

Reputation: 37073

code in the task definition is execute whenever this it is "read" if you want to have actions done, when the task runs make "last". (<< is shorthand for doLast) E.g.

task toTest() {
        project.ext.set('deployType', 'test')
}
toTest << {
        println "Set deploy type: ${project.deployType}"
}
task showStatus() {
        mustRunAfter = ['toTest']
}
showStatus << {
        println "Curr deploy type: ${project.deployType}"
}
task lerl(dependsOn: [toTest, showStatus]) {
        println "lerl"
}

Gives

lerl
:toTest
Set deploy type: test
:showStatus
Curr deploy type: test
:lerl

Upvotes: 1

Related Questions