Gradle delete task failing with “unable to delete

2019-08-05 10:24发布

问题:

I have the following delete task added to my build.gradle file:

task cleanExtra(type: Delete) {
    delete '../version.properties'
}

clean.dependsOn(cleanExtra)

Sometimes, when I call gradle clean, it will fail with "Unable to delete file: (...)/version.properties". But if I call the same clean task a second time, it will successfully delete the task.

Why does this happen? Anyway to avoid it?

回答1:

It sounds like JIRA issue Gradle-2244 :

Unable to delete file/directory, and then a subsequent clean will succeed.

From the last comment:

After some research, it appears that this may be due to a bug in the Windows JDKs (incl IBM). Ant uses a strategy of forcing a GC after a failed delete and then waiting a small amount of time. Given that this appears to be a successful strategy for Ant, we have adopted it.

This issue is marked as fixed in version 1.1-rc-1. Presumably the behaviour you observe is "the fix", which a workaround for a JDK bug. So I'd guess that this behaviour may occur in later versions of Gradle.