Gradle : How to read all files in a directory using FileTree
Introduction to FileTree in Gradle Api
FileTree is an interface that extends FileCollection and introduces methods like matching() and visit(). It class represents a hierarchy of files in a given directory. It includes files in same folder and all sub folders.
How to get all files in a Directory
To load all files in a given directory and its sub directories you can use FileTree. Syntax for FileTree is as shown below.
FileTree tree = fileTree('path/to/dir')
The FileTree can get files in following ways
- from relative path
 - from
 
We can write a gradle task to list all the files in the src folder as shown below.
task loadFiles(){
    doLast{
        FileTree tree = fileTree('src')
        tree.each {
            println it.name
        }
    }
}
When we run the aobe task using command gradle -q loadFiles it will give the following output
sample1.txt
sample2txt
file1.properties
A.java
No comments: