Delete task in execution phase

Hi,

I would like to ask you for help. I somehow don’t understand, how to leverage Gradle’s Delete task in execution phase.

task T1(dependsOn: T2)
<< {
    // some behaviour
}
  task T2(type: Delete) << {
   delete 'myTestFile.txt'
   // since this is in execution phase, it won't work as expected
}

But I don’t want to do this:

task T2(type: Delete) {
   // no matter what task is executed, this is executed always :-(((((
   // I need it to bind to T1 task, which is run in execution (if user invokes it)
}

What you want is:

task T2(type: Delete) {
   delete 'myTestFile.txt'
}

Because T2 is a Delete task, the ‘delete’ method in the configuration closure we have there means something different to what it would if it were not a Delete task.

In this case, you are specifying what should be deleted when the task runs. That is, you are configuring the delete task. If the task was not a delete task, the delete method would actually be ‘Project.delete()’ which deletes the file right at that time.

Yes, I think I have understanding of that. Problem is, that I want my delete task to be run only if particular execution phase task is run

example:

task T2(type: Delete)
{
   delete 'myTestFile.txt'
}
  task T0()
<< {
    // some behaviour
}
  task T1(dependsOn: T2)
<< {
    // some behaviour which needs to invoke T2 first
}

If I invoke

gradle T1

It’ll be error like

Could not find property 'T2' on root project ...

And I only want my delete task T2 to be run IF task T1 is run. Otherwise it should not do anything else.

I’m not sure, if this is possible or I should rather write some helper task, which would delete desired directory/file for me in execution phase.

If you want to delete something in execution phase, you an just do:

project.delete(someFile)

Have a look at the docs for ‘Project.delete()’.

ashamed

Your solution makes perfect sense. I’ve been so focused on Delete task and config. phase, that I completely overlooked this.

Thanks for help

No probs, happy Gradling.