Deleting a dir with delete or Project.delete() in a task does not work. Works in a doLast section though


(Bessels) #1

Caveat emperor: New to gradle. Did some RTFM and googled but can’t seem to find the answer wrt what I’m doing wrong. Part of a script to create a war and bundle it with a tomcat server. All seems to work except for deleting of a certain directory.

task dist(type: Zip, dependsOn: ‘extract’) {
description = “Build distribution ZIP”

setArchiveName("ourDistribution.zip")
from "${distsDir}/unpacked"
into ('.')
delete "${distsDir}/unpacked" // does NOT delete the unpacked directory

}

A workaround which does work in this case because delete is called last in the dist task.

task dist(type: Zip, dependsOn: ‘extract’) {
description = “Build distribution ZIP”

setArchiveName("ourDistriubion.zip")
from "${distsDir}/unpacked"
into ('.')

}

dist.doLast {
delete “${distsDir}/unpacked”
}

Note: I’ve also tried replacing delete with project.delete ("${distsDir}/unpacked") to no avail. Like delete it does not work in situation 1 but works with the .doLast.

I’m running it with gradlew dist (using 2.12 of the wrapper). I’m very interested in what I’m doing wrong and why the delete command does not do what I expected it to do in the first example and why it does work with the doLast.

Also setArchiveName sets the name of the distribution. The zip task/plugin does only have a archiveName property. The API documentation however mentions a setArchiveName method.Is this the correct way of setting the name of the zip file?


(Stefan Oehme) #2

In your first example, you are calling delete() during the configuration phase. This is when you set up the properties of your task. This means it is called while Gradle is evaluating the build script, making every Gradle execution slower.

What you want and what your second example does correctly is to delete during the execution phase when the task is actually run. This is exactly what doLast is for.