How to share a property between multiple tasks of the same plugin


(Martin d'Anjou) #1

My plugin has two tasks but I don’t know how to specify a property ONCE and have it change both tasks. The code is:

class Step1 extends DefaultTask {
    Map<String,?> env
    @TaskAction
    public void perform() {
        println env
    }
}

class Step2 extends DefaultTask {
    Map<String,?> env
    @TaskAction
    public void perform() {
        println env
    }
}

class MultiStep implements Plugin<Project> {
    def env = ["VAR" : "value"]
    void apply(Project project) {
        Task step1 = project.tasks.create('step1', Step1)
        Task step2 = project.tasks.create('step2', Step2)
        step1.env = env
        step2.dependsOn step1
        step2.env = env
    }
}
apply plugin: MultiStep

How do I pass a new env value ONCE to both tasks? Of course, this fails:

MultiStep {
    env = ['VAR2':'value2']
}

I want to avoid global variables like ext. I could create a third task that would only hold common configuration values for both tasks, but I wonder if there is a better way. Thanks.


(Martin d'Anjou) #2

Answering my own question. Use a plugin extension, and conventionMapping. I found the answer in chapter 8 of Gradle In Action. Why I even forgot to look in there beats me.