简体   繁体   中英

Pass variable back from Groovy class to Gradle task

I might not have set the right title for this question and that's because i'm not that advanced in Gradle, so apologies for that.

I have the below Gradle tasks in my build.gradle:

// buildSrc/src/main/groovy/envs/actions  - This where all the groovy classes which I use are located
import envs.actions.*

tasks.create("createEnvironment", CreateApplicationEnvironmentTask) {
    println "executing creation"
  }



return tasks.create("createRecord", CreateRecordTask) {
     dependsOn  "createEnvironment"
     varEbCname = tasks["createEnvironment"].ebCname
  }

The first task is of type "CreateApplicationEnvironmentTask", which is a class in a .groovy file where i do some actions and set some variables.

One of these variables that i set in the "CreateApplicationEnvironmentTask" class is also one called "ebCname", as seen below:

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

class CreateApplicationEnvironmentTask extends DefaultTask {
    String ebCname

    @TaskAction
    def create() {
      ebCname = "some_value"
    }
}

What I'm looking to do is to be able to get the value of the variable "ebCname"(which is set inside the class) from the second task "createRecord" by calling the "createEnvironment" task. I'm doing the below from within the second task, but it's not working:

varEbCname = tasks["createEnvironment"].ebCname

"varEbCname" ends up being null.

I also tried "return"-ing the "ebCname" variable from the class, but that didn't work either.

The "ebCname" variable is set in the "CreateApplicationEnvironmentTask" class which is used by the "createEnvironment" task, so I need a way get the value of that variable from the second task "createRecord".

Any idea why this isn't working and how would I go about doing this?

Thanks,

I think the main confusion here is between the gradle task life cycle phases . This is quite common when starting out with gradle. I would recommend reading through the above link to get a feel for the phases.

To illustrate here is a complete build.gradle which more or less mirrors your code:

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

task createEnvironment(type: CreateApplicationEnvironmentTask) {
  println "configuring CreateApplicationEnvironmentTask"
  doLast { 
    println "execution phase (doLast) CreateApplicationEnvironmentTask"
  }
}

task createRecord(type: CreateRecordTask, dependsOn: ['createEnvironment']) {
  println "configuration phase value:  ${tasks["createEnvironment"].ebCname}"
  doLast { 
    println "execution phase (doLast) value:  ${tasks["createEnvironment"].ebCname}"
  }
}

class CreateApplicationEnvironmentTask extends DefaultTask {
    String ebCname

    @TaskAction
    def create() {
      println "execution phase (class) CreateApplicationEnvironmentTask"
      ebCname = "some_value"
    }
}

class CreateRecordTask extends DefaultTask { 

  @TaskAction 
  def doit() {
    println "executin phase (class) CreateRecordTask"
  }
}

executing:

~> gradle createRecord

prints:

configuring CreateApplicationEnvironmentTask
configuration phase value:  null
:createEnvironment
execution phase (class) CreateApplicationEnvironmentTask
execution phase (doLast) CreateApplicationEnvironmentTask
:createRecord
executin phase (class) CreateRecordTask
execution phase (doLast) value:  some_value

BUILD SUCCESSFUL

when you execute any gradle task on a build.gradle file (or even just execute gradle tasks to print the available tasks), the build file is evaluated and the configuration phase code is run. This includes the code inside the curlies of any task foo { ... } definition, even if that task is not executed.

In contrast, the code inside the doLast block task foo { doLast { ... }} is executed in the execution phase.

The reason you were seeing a null value is that you set the property ebCname in your task action method (execution phase), but you are printing it in the configuration phase. Thus it has not been set yet. Either setting the value in the configuration phase (in the constructor of the class, directly when you declare the field, of inside the task declaration

task createRecord(type: CreateRecordTask...) { 
    ebCname = "foo"
}

) or referring to the value in the execution phase will give you a non-null value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM