api / org.gradle.api.file / FileTree

FileTree

interface FileTree : FileCollection

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive.

You can obtain a FileTree instance using org.gradle.api.Project#fileTree(java.util.Map), org.gradle.api.Project#zipTree(Object) or org.gradle.api.Project#tarTree(Object).

Functions

getAsFileTree

abstract fun getAsFileTree(): FileTree

Returns this.

getFiles

abstract fun getFiles(): MutableSet<File>

Returns the contents of this tree as a flattened Set.

matching

abstract fun matching(filterConfigClosure: Closure<Any>): FileTree

Restricts the contents of this tree to those files matching the given filter. The filtered tree is live, so that any changes to this tree are reflected in the filtered tree.

The given closure is used to configure the filter. A org.gradle.api.tasks.util.PatternFilterable is passed to the closure as its delegate. Only files which match the specified include patterns will be included in the filtered tree. Any files which match the specified exclude patterns will be excluded from the filtered tree.

abstract fun matching(filterConfigAction: Action<in PatternFilterable>): FileTree

Restricts the contents of this tree to those files matching the given filter. The filtered tree is live, so that any changes to this tree are reflected in the filtered tree.

The given action is used to configure the filter. A org.gradle.api.tasks.util.PatternFilterable is passed to the action. Only files which match the specified include patterns will be included in the filtered tree. Any files which match the specified exclude patterns will be excluded from the filtered tree.

abstract fun matching(patterns: PatternFilterable): FileTree

Restricts the contents of this tree to those files matching the given filter. The filtered tree is live, so that any changes to this tree are reflected in the filtered tree.

The given pattern set is used to configure the filter. Only files which match the specified include patterns will be included in the filtered tree. Any files which match the specified exclude patterns will be excluded from the filtered tree.

plus

abstract fun plus(fileTree: FileTree): FileTree

Returns a FileTree which contains the union of this tree and the given tree. The returned tree is live, so that changes to either this tree or the other source tree are reflected in the returned tree.

visit

abstract fun visit(visitor: FileVisitor): FileTree

Visits the files and directories in this file tree. Files are visited in depth-first prefix order, so that a directory is visited before its children.

abstract fun visit(visitor: Closure<Any>): FileTree

Visits the files and directories in this file tree. Files are visited in depth-first prefix order, so that a directory is visited before its children. The file/directory to be visited is passed to the given closure as a

abstract fun visit(visitor: Action<in FileVisitDetails>): FileTree

Visits the files and directories in this file tree. Files are visited in depth-first prefix order, so that a directory is visited before its children. The file/directory to be visited is passed to the given action as a

Inherited Functions

add

abstract fun add(collection: FileCollection): FileCollection

Adds another collection to this collection. This is an optional operation.

addToAntBuilder

abstract fun addToAntBuilder(builder: Any, nodeName: String, type: AntType): Unit

Adds this collection to an Ant task as a nested node. The given type determines how this collection is added:

  • AntType#MatchingTask: adds this collection to an Ant MatchingTask. The collection is converted to a set of source directories and include and exclude patterns. The source directories as added as an Ant Path with the given node name. The patterns are added using 'include' and 'exclude' nodes.
  • AntType#FileSet: adds this collection as zero or more Ant FileSets with the given node name.
  • AntType#ResourceCollection: adds this collection as zero or more Ant ResourceCollections with the given node name.
You should prefer using AntType#ResourceCollection, if the target Ant task supports it, as this is generally the most efficient. Using the other types may involve copying the contents of this collection to a temporary directory.

abstract fun addToAntBuilder(builder: Any, nodeName: String): Any

Adds this collection to an Ant task as a nested node. Equivalent to calling addToAntBuilder(builder, nodeName,AntType.ResourceCollection).

asType

abstract fun asType(type: Class<*>): Any

Converts this collection into an object of the specified type. Supported types are: Collection, List, Set, Object[], File[], File, and FileTree.

You can call this method in your build script using the as operator.

contains

abstract fun contains(file: File): Boolean

Determines whether this collection contains the given file. Generally, this method is more efficient than calling getFiles().contains(file).

filter

abstract fun filter(filterClosure: Closure<Any>): FileCollection

Restricts the contents of this collection to those files which match the given criteria. The filtered collection is live, so that it reflects any changes to this collection.

The given closure is passed the File as a parameter, and should return a boolean value.

abstract fun filter(filterSpec: Spec<in File>): FileCollection

Restricts the contents of this collection to those files which match the given criteria. The filtered collection is live, so that it reflects any changes to this collection.

getAsPath

abstract fun getAsPath(): String

Returns the contents of this collection as a platform-specific path. This can be used, for example, in an Ant <path> element.

getSingleFile

abstract fun getSingleFile(): File

Returns the content of this collection, asserting it contains exactly one file.

isEmpty

abstract fun isEmpty(): Boolean

Returns true if this collection is empty. Generally, calling this method is more efficient than calling getFiles().isEmpty().

minus

abstract fun minus(collection: FileCollection): FileCollection

Returns a FileCollection which contains the difference between this collection and the given collection. The returned collection is live, and tracks changes to both source collections.

You can call this method in your build script using the - operator.

plus

abstract fun plus(collection: FileCollection): FileCollection

Returns a FileCollection which contains the union of this collection and the given collection. The returned collection is live, and tracks changes to both source collections.

You can call this method in your build script using the + operator.

stopExecutionIfEmpty

abstract fun stopExecutionIfEmpty(): FileCollection

Throws a StopExecutionException if this collection is empty.

Inheritors

ConfigurableFileTree

interface ConfigurableFileTree : FileTree, DirectoryTree, PatternFilterable, Buildable

A FileTree with a single base directory, which can be configured and modified.

You can obtain a ConfigurableFileTree instance by calling org.gradle.api.Project#fileTree(java.util.Map).

SourceDirectorySet

interface SourceDirectorySet : FileTree, PatternFilterable, Named, Describable

A SourceDirectorySet represents a set of source files composed from a set of source directories, along with associated include and exclude patterns.

SourceDirectorySet extends FileTree. The contents of the file tree represent the source files of this set, arranged in a hierarchy. The file tree is live and reflects changes to the source directories and their contents.