Execution time variable setting

I’m trying to set a global variable at execution time to use depending on the selected task:

def taskName = ‘’

task taskA() {
taskName = 'taskA’
dependsOn “printTaskName”

task taskB() {
taskName = 'taskB’
dependsOn “printTaskName”

task printTaskName() {
println taskName


Sorry, my original post was incomplete:

The above code does not work because the taskName assignments occur at configuration time.

I need a mechanism that changes the value of taskName at execution time.

Thanks again.