Issue in setting a variable in one task and use in another

Hi

I have created a custom gradle plugin. and ave created two tasks:; task1 task2

I want to create and set a value to the variable in task1 and used that value in task2. can anyone please let me know, ow to do that? i tried

project.ext.a=10

But, when i try to access that in task2, it says it does not ave the property can anyone let me know

defaultTasks = ['task2']
  task task1 {
 doLast {
  project.ext.a = 10
 }
}
  task task2 {
 dependsOn 'task1'
 doLast {
  println project.ext.a
 }
}

It’s hard to say where you went wrong without seeing your code, but this runs for me.

can anyone please let me know, how to resolve this

I’ve already replied to this once, and it looks like you have as well. I wonder if the forum server is misbehaving.

This works for me:

defaultTasks = ['task2']
  task task1 {
 doLast {
  project.ext.a = 10
 }
}
  task task2 {
 dependsOn 'task1'
 doLast {
  println project.ext.a
 }
}

If this doesn’t resolve your issue, post an example of your code.

Hi,

project.task('release', description: 'Verify project, release, and tag the version.',
group: RELEASE_GROUP, dependsOn 'task3',type: GradleBuild) {
startParameter = project.getGradle().startParameter.newInstance()
     tasks = ['task1','task2']
 }
project.task('task1', group: RELEASE_GROUP,
   description: 'task1') << this.&task1
  project.task('task2', group: RELEASE_GROUP,
  description: 'task2') << this.&task2
  void task1{
println project.ext.a
}
void task2{
println project.ext.a
}
project.task('task3', group: RELEASE_GROUP,
  description: 'task3') << this.&task3
      void task3{
project.ext.a =10
}

In the above code, i have three tasks : task1, task2, task3. Task3 willl set the variable. And, task1 and task 2 will use this. Also, both these tasks are grouped. If i add te dependson to the taskgroup , it does not work. If i add task3 on task1 and task2, then it works.

can you let me now, how to resolve tis.

Can you let me know, if we can do this on grouping a task. If so,how to do that

I’m not sure what you’re trying to do here. Is this in a “.gradle” file or in a custom plugin?

I think all you’re doing here is this:

defaultTasks = ['release']
def RELEASE_GROUP = "release"
  task release {
 description = "Verify project, release, and tag the version."
 group = RELEASE_GROUP
 dependsOn 'task1'
 dependsOn 'task2'
}
  task task1 {
 description = "task1"
 group = RELEASE_GROUP
 dependsOn 'task3'
    doLast {
  println project.ext.a
 }
}
  task task2 {
 description = "task2"
 group = RELEASE_GROUP
 dependsOn 'task3'
    doLast {
  println project.ext.a
 }
}
  task task3 {
 description = "task3"
 group = RELEASE_GROUP
    doLast {
  project.ext.a = 10
 }
}

The order in which task1 and task2 are executed is arbitrary in this code. If task1 must run before task2 you can either add “task2.dependsOn ‘task1’” or “task2.mustRunAfter ‘task1’”.

I suspect that it would help to read up on the DAG. Gradle task interactions are all about setting up dependencies and letting gradle set up the task graph accordingly.

Rereading this, it looks like it is in a plug-in. the code is basically the same, except you’ll use

project.task("task1") << this.&task1

Everything else is the same. I haven’t used the redirect to a method for awhile, so the syntax might not be right. But I’m pretty sure that the issue you’re having with with dependency ordering.

Also, the group concept is only for documentation. It doesn’t have any affect on task ordering.

Thanks. it worked