Gradle : Difference between File, FileCollection and FileTree in Gradle

Difference Between File, FileCollection and FileTree in Gradle

File FileCollection FileTree
File is from java.io package FileCollection is from org.gradle.api.file package FileTree is from org.gradle.api.file package
File represents a single file. FileCollection represents a collection of files FileTree represents hierarchy of files in a directory or folder.

Difference Between File, FileCollection and FileTree

Suppose we have a directory structure as shown in image below.

File Structure

We can write a gradle task as shown below


task loadFiles(){
    doLast{

        File file = file('src/main/resources/sample1.txt')
        println file.name
        println "\n"

        FileCollection collection = files('src/main/resources/sample1.txt','src/main/resources/sample2.txt')
        collection.each {
            println it.name
        }
        println "\n"

        FileTree tree = fileTree('src')
        tree.each {
            println it.name
        }
    }
}

When we run the above gradle task using command gradle -q loadFiles, it will give the following output.

sample1.txt


sample1.txt
sample2.txt


sample1.txt
sample2txt
file1.properties
A.java

Read More

No comments :

Post a Comment