Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting directories via gradle FileTree.include

Tags:

gradle

I want to say:

clean.delete(fileTree("a") {
   include "subdir/"
   include "aFile"
})

to delete the directory "subdir" and the file "aFile". But "subdir" is not deleted. I could list it explicitly:

clean.delete("a/subdir")

but that is more repetitious than I would like. Can fileTree.include be made to do this job?

So far I have come up with:

[ "subdir",
  "aFile",
  ...
].each { it -> 
   clean.delete("a/$it") 
}

but that is just a bit awkward.

like image 308
kevin cline Avatar asked Jun 17 '14 23:06

kevin cline


2 Answers

I am afraid this is not possible. See this discussion on the gradle forum. The discussion lead to this issue.

A simple test shows

task makeDir << {
    ['a', 'a/subdir'].each { new File(it).mkdirs() }
    new File('a/aFile').createNewFile()

    def tree = fileTree('a') {
        include 'subdir/'
        include 'aFile'
    }

    tree.each {File file ->
        println file
    }
}

prints only the file and not the directory, as the directory is traversed. Your solution is good but you could save some chars:

[ "subdir",
  "aFile",
  ...
].each { clean.delete("a/$it") }

So fileTree is just for files in a directory tree and not for directories.

like image 78
ChrLipp Avatar answered Oct 04 '22 19:10

ChrLipp


You can specify it this way:

clean {
   delete 'a/aFile', 'a/subdir'
}

No idea why fileTree doesn't work.

like image 37
Opal Avatar answered Oct 04 '22 18:10

Opal