Gradle : How to list files inside a Jar, Zip Tar Files in Gradle Task
How to load and read files in Gradle Project.
The Gradle Project Api has the following methods for using files.
file()- to load and read a particular filefiles()- to load and read multiple filesfileTree()- to load and read file hierarchy in a directory or folderzipTree()- to load and read a zip file (includes Jar , War and Ear files)tarTree()- to load and read a tar file
How to list files inside a Zip file Jar File and Tar file
Lets say we have a file structure as shown in image below.
Following Gradle task will list the files inside a zip file, jar file and a tar file at the location "src/main/resources/archives/"
task showFiles(){
    doLast{
        FileTree zipFiles = zipTree("src/main/resources/archives/resources.zip");
        FileTree jarFiles = zipTree("src/main/resources/archives/resources.jar");
        FileTree tarFiles = tarTree("src/main/resources/archives/resources.tar.gz");
        println " \n#Contents of Zip File"
        zipFiles.each {
            println "${it.name}"
        }
        println " \n#Contents of Jar File"
        jarFiles.each {
            println "${it.name}"
        }
        println " \n#Contents of Tar File"
        tarFiles.each {
            println "${it.name}"
        }
    }
}
When we run the above gradle task using command gradle -q showFiles, it will give the following output.
 
#Contents of Zip File
file1.properties
sample1.txt
sample2txt
 
#Contents of Jar File
file1.properties
sample1.txt
sample2txt
 
#Contents of Tar File
sample1.txt
file1.properties
sample2txt
Read More
No comments: