Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Delete not working

Tags:

gradle

I want to append some extra delete functionality to the clean task (for Java builds).

So I try adding the following to my gradle build script.

clean.doLast{
    delete ('test.txt')
}

When I tun the "clean" task my sample file doesn't get deleted ... I also don't get any error message indicated what happened.

If I try the following:

task deleteStuff(type: Delete) {
    delete 'test.txt'
}

Things do work.

Can I not add (via doLast) delete functionality to tasks? What is the proper way of doing this (without hacking in Ant tasks).

like image 961
vicsz Avatar asked Mar 13 '12 00:03

vicsz


People also ask

How do I delete a task in Gradle?

The long answer: Your first example mixes the two ways to delete files in Gradle. The first one is to use a task of type Delete , the second one is to invoke the method delete of the type Project .

What is Gradle clean task?

Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with the outputs property.

How to stop Gradlew run?

You can call . \gradlew --stop in a Terminal and it will kill all gradle processes in Android Studio for Windows OS. You Can find terminal at left bottom of your android studio for windows. Save this answer.


2 Answers

In these two code snippets, you aren't calling Project.delete() but Delete.delete(). In other words, you are configuring the Delete task. Doing this after the Delete task has executed (as in the first snippet) is too late.

In the case of a Delete task, there is no good reason to add a delete operation with doLast. Your second snippet is clearly preferable. For other tasks, the doLast approach will work because they don't have a delete method. Or you can disambiguate with project.delete().

like image 192
Peter Niederwieser Avatar answered Oct 25 '22 09:10

Peter Niederwieser


If the reason is to avoid spending time in the preparation phase (which is executed for every task), doFirst can be used to set up the Delete-task. So using clean.doFirst instead of clean.doLast in the example above will work.

like image 30
Alexander Haas Avatar answered Oct 25 '22 09:10

Alexander Haas