Delete task issue, improvement needed imho


(fladnag) #1

Hi,

I spent some hours on an unexpected result with Delete task.

I have a folder and I want to delete all simple files in folder root but not the folder itself or folders in folder, so I wrote :

ext.folder = 'test'
task cleanContentFolder(type:Delete) {
   file(ext.folder).listFiles().findAll{ !it.isDirectory() }.each {
      delete it 
   }
}

and… it’s almost works : files are deleted at configuration phase, when task is called or not.

I finally understand the “delete” keyword, interpreted in the each closure, is resolved to org.gradle.api.internal.project.AbstractProject.delete and not org.gradle.api.tasks.Delete.delete.

So… I resolve my issue with :

task cleanContentFolder(type:Delete) { deleteTask ->
   file(ext.folder).listFiles().findAll{ !it.isDirectory() }.each {
      deleteTask.delete it 
   }
}

but I think some improvements could be made to avoid this happening, for example rename “delete” method of Delete task to “deleteElement” or something else.


(Lance Java) #2

I’d likely solve this using a FileTree

task cleanContentFolder(type:Delete) {
   def deleteMe = fileTree(ext.folder).matching {
      include { FileTreeElement element -> 
         return !element.directory 
      }
   }
   targetFiles = deleteMe
}

@see FileTree.matching(groovy.lang.Closure)
@see PatternFilterable.include(groovy.lang.Closure)