Passing and changing variable from one task to another

Hello Gradle community!

My question is exactly as this one Passing variables from one task to another - #3 by Joe_Singray
I have read through it and through documentation link given in that topic but unfortunately I am unable to come up with a solution so I am in need of help.

var string = ""

task1 {
  string = someCalculation()
}

//now that I have stored value from task1, I want to use it in task2

task2{
  val stringTask2 = string
  doSomething(stringTask2)
}

But what happens is string variable is empty, in other words it doesnt have value from task1.
I understand this happends due to gradle build lifecycle. First tasks go through configure phase, and then in execute phase they use variables that were declared/given value in configure phase.
How should I go about solving this problem?
Thanks ahead!

You say you question is exactly what is in that other question, but that’s a case where the value is set during task execution of one task, but used during configuration of the second task, which is too late. The code you’ve shown for your example would do everything in the configuration phase so it wouldn’t have the issue you’re saying that you have.

This is pretty straight forward to solve now with properties / providers that weren’t available when that post was written, but it’s hard to give you a good example of what you should do for your case when your example provided is actually simplifying out the significant parts of what you’re trying to do.

@jjustinic thanks for taking interest in my question. I am sorry if it was unclear, I will try to clear it up a bit more.
My question is exactly the same as linked.

//declaring variable string
var string = ""

//in task1 i want to do some calculation and store some other value to string variable
//in order for it to be shared with task2
//lets say value of someCalculation() is = "success"
task1 {
  string = someCalculation()
}

//here I want to use string variable of value = "success" but instead i get an empty string
task2{
  doSomething(string)
}

variable string is empty in task2 because string value “success” is given to variable string during execution phase. So task2 has no idea variable string has changed from its initial value (empty) given in configuration phase.
Tasks are already dependant (task1 is executed first, task2 second)
So i would need help in solving this, i have read through docs but have no idea how to access this.

This still has a conflict, but I think you’re saying you actually have something more like this:

var string = ""

task1 {
  doLast {
    string = someCalculation()
  }
}

task2 {
  configureSomething(string)
} 

Here, the doLast is pretty critical in showing what you have as all code in your original example ran in the configuration phase. Without a task action, nothing ran in the execution phase, so string would not have the problem in the linked question. I’m also expecting that your task2 usage is actually a method that configures something used in the execution phase, but that might not be the case. These seem like minor details, but are critically important to giving you a real code example of what you should do.

1 Like

A post was split to a new topic: Trouble with Git Exec Tasks