How to delete an empty directory (or the directory with all contents recursively) in gradle?

前端 未结 6 1419
后悔当初
后悔当初 2020-12-15 18:10

I can\'t figure out how to delete all contents of a directory.

For cleaning out a directory, I want to remove all files and directories inside it: I want to wipe ev

相关标签:
6条回答
  • 2020-12-15 18:50

    Following will delete all content from the src folder but leaves the folder itself untouched:

    task deleteGraphicsAssets(type: Delete) {
        def dirName = "src" 
        file( dirName ).list().each{
            f -> 
                delete "${dirName}/${f}"
        }
    }
    
    0 讨论(0)
  • 2020-12-15 18:59

    To delete the src directory and all its contents:

    task deleteGraphicsAssets(type: Delete) {
        delete "src"
    }
    
    0 讨论(0)
  • 2020-12-15 19:00

    Groovy enhances the File class with several methods. You can delete a directory with all it's subdirectories and files using deleteDir() method.

    task deletebin << {
        def binDir = new File('bin')
        binDir.deleteDir()
    }
    
    0 讨论(0)
  • 2020-12-15 19:01
    clean {
        delete += fileTree('src').include('**/*')
    }
    

    This 'clean' task's configuration seems to work.

    0 讨论(0)
  • 2020-12-15 19:02

    Found using FileTree#visit worked.

    ConfigurableFileTree ft = fileTree('someDir')
    ft.include("xxx")
    ft.exclude("yyy")
    
    task delteFilesOnly() {
        doLast {
            //// for test
            //ft.each { File file ->
            //    println "===== " + file.absolutePath
            //}
            delete ft
        }
    }
    
    task deleteFilesAndDirs(){
        doLast {
            ft.visit { FileVisitDetails fvd ->
                //// for test
                //println "----- " + file.absolutePath
                delete fvd.file
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 19:09

    At the risk of resurrecting an answered topic, there's a relatively easy way to do this.

    This task will delete all files and directories under 'src' without traversing the file tree and without removing to 'src' dir

    task deleteGraphicsAssets(type:Delete) {
        delete file('src').listFiles()
    }
    
    0 讨论(0)
提交回复
热议问题