api / org.gradle.api.file / FileCollection

FileCollection

interface FileCollection : MutableIterable<File>, AntBuilderAware, Buildable

A FileCollection represents a collection of files which you can query in certain ways. A file collection is often used to define a classpath, or to add files to a container.

You can obtain a FileCollection instance using org.gradle.api.Project#files.

Types

AntType

class AntType

Ant types which a FileCollection can be mapped to.

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.

getAsFileTree

abstract fun getAsFileTree(): FileTree

Converts this collection to a FileTree. Generally, for each file in this collection, the resulting file tree will contain the source file at the root of the tree. For each directory in this collection, the resulting file tree will contain all the files under the source directory.

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.

getFiles

abstract fun getFiles(): MutableSet<File>

Returns the contents of this collection as a Set.

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.

Inherited Functions

getBuildDependencies

abstract fun getBuildDependencies(): TaskDependency

Returns a dependency which contains the tasks which build this artifact. All Buildable implementations must ensure that the returned dependency object is live, so that it tracks changes to the dependencies of this buildable.

Inheritors

ConfigurableFileCollection

interface ConfigurableFileCollection : FileCollection

A ConfigurableFileCollection is a mutable FileCollection.

You can obtain an instance of ConfigurableFileCollection by calling

Configuration

interface Configuration : FileCollection, HasConfigurableAttributes<Configuration>

A Configuration represents a group of artifacts and their dependencies. Find more information about declaring dependencies to a configuration or about managing configurations in docs for ConfigurationContainer

Configuration is an instance of a FileCollection that contains all dependencies (see also #getAllDependencies()) but not artifacts. If you want to refer to the artifacts declared in this configuration please use #getArtifacts() or #getAllArtifacts(). Read more about declaring artifacts in the configuration in docs for org.gradle.api.artifacts.dsl.ArtifactHandler Please see the Managing Dependency Configurations User Guide chapter for more information.

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).

SourceSetOutput

interface SourceSetOutput : FileCollection

A collection of all output directories (compiled classes, processed resources, etc.) - notice that SourceSetOutput extends FileCollection.

Provides output information of the source set. Allows configuring the default output dirs and specify additional output dirs.

 apply plugin: 'java' sourceSets { main { //if you truly want to override the defaults: output.resourcesDir = file('out/bin') // Compiled Java classes should use this directory java.outputDir = file('out/bin') } } 
Working with generated resources.

In general, we recommend generating resources into folders different than the regular resourcesDir and classesDir. Usually, it makes the build easier to understand and maintain. Also it gives some additional benefits because other Gradle plugins can take advantage of the output dirs 'registered' in the SourceSet.output. For example: Java plugin will use those dirs in calculating class paths and for jarring the content; IDEA and Eclipse plugins will put those folders on relevant classpath.

An example how to work with generated resources:

 apply plugin: 'java' def generatedResources = "$buildDir/generated-resources/main" sourceSets { main { //let's register an output folder on the main SourceSet: output.dir(generatedResources, builtBy: 'generateMyResources') //it is now a part of the 'main' classpath and will be a part of the jar } } //a task that generates the resources: task generateMyResources { doLast { def generated = new File(generatedResources, "myGeneratedResource.properties") generated.text = "message=Stay happy!" } } //Java plugin task 'classes' and 'testClasses' will automatically depend on relevant tasks registered with 'builtBy' //Eclipse/IDEA plugins will automatically depend on 'generateMyResources' //because the output dir was registered with 'builtBy' information apply plugin: 'idea'; apply plugin: 'eclipse' 
Find more information in #dir(java.util.Map, Object) and #getDirs()