Introduction to FileCollection
FileCollection is an interface that extends
How to load files in a FileCollection
Suppose we have a file structure as shown in image below
We can load multiple files for operation using the following syntax
FileCollection collection = files('relative/path/file1','absolute/path/file2',new File('file3'))
As shown in the example above we can load files in three ways
- From a relative path
- From a absolute path
- From a File object.
We can write a gradle task to read all files in a folder or directory as follows
FileTree tree = fileTree('src')
Now when we run the above gradle task with the command
gradle -q fileTree, it will give the
Operations and methods available for FileCollection
getSingleFile()- It returns a single file. (it will throw IllegalStateException if files contains more than one file or no file)
getAsFileTree()- It returns the FileCollection as a FileTree.
plus(FileCollection collection)- It will add the specified file collection to the existing file collection.
minus(FileCollection collection)- It will remove from the collection files in specified collection.
filter(Closure filter)- It will filter the files from collection and make changes to the same collection.