Two tasks, shared variable


(Simo Amini) #1

I have two tasks that are executed one after the other, in the first I set a variable that I use in the second:

project.ext.set(“chain”, “empty”)
task swid {
project.ext.set(“chain”, “variant”)
}
task compile{
def propC = project.chain
println propC
}

I always get : empty
as a result

how can I change the variable value in the first task?


(James Justinic) #2

The code provided will always return variant, not empty, so you might need to double check what you have provided. The code in both your tasks should be wrapped in a doLast { } to execute with the task rather than at configuration time.

project.ext.set('chain', 'empty')
  
task swid {
    doLast {
        project.ext.set('chain', 'variant')
    }
}

task compile {
    doLast {
        def propC = project.chain
        println propC
    }
}

If your description is accurate, it seems likely that you’re halfway there and swid has doLast { }, but compile does not.


(Simo Amini) #3

yes I missed the doLast{}, now it’s working perfectly