bearhunterUA bearhunterUA - 3 months ago 27
Groovy Question

Can not delete directory by mask in Gradle

I try to delete directory in 'foo/dir' with name 'public-someHash'. 'SomeHash' was created dynamically (eg 'dsflsdfn') and always new. I tried to use 'fileTree' but directory still present. There is my code:

tasks.create(name: 'delete', type : Delete) {
delete fileTree(dir: 'foo/dir/', include: 'public-*/**')
}


What is wrong with my mask?

UDP: I have similar task in Ant and all works fine:

<target name="delete">
<delete includeemptydirs="true">
<fileset dir="foo/dir/">
<include name="public-*/**"/>
</fileset>
</delete>
</target>

Answer

EDIT: Apologies, as original answer was based on a misreading of the question.

Here's one way to do it, but not the most elegant:

task myDelete(type: Delete) {
    def files = new HashSet()

    new File('foo/dir').eachFile { file ->
        if (file.isDirectory() && (file.name ==~ /public-.*/)) {
            files << file
        } 
    }

    delete files
}
Comments