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

File Structure

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

  1. from relative path
  2. from

We can write a gradle task to list all the files in the src folder as shown below.

task loadFiles(){
FileTree tree = fileTree('src')
tree.each {

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