api / org.gradle.api.tasks / SourceSetOutput

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

Functions

dir

abstract fun dir(options: MutableMap<String, Any>, dir: Any): Unit

Registers an extra output dir and the builtBy information. Useful for generated resources.

See example at SourceSetOutput

abstract fun dir(dir: Any): Unit

Registers an extra output dir. Useful for generated resources.

See example at SourceSetOutput

getClassesDir

abstract fun getClassesDir(): File

Returns the directory to assemble the compiled classes into.

See example at SourceSetOutput

getClassesDirs

abstract fun getClassesDirs(): FileCollection

Returns the directories containing compiled classes.

getDirs

abstract fun getDirs(): FileCollection

Returns all dirs registered with #dir method. Each file is resolved as org.gradle.api.Project#file(Object)

See example at SourceSetOutput

getResourcesDir

abstract fun getResourcesDir(): File

Returns the output directory for resources

See example at SourceSetOutput

isLegacyLayout

abstract fun isLegacyLayout(): Boolean

Source set uses the legacy layout (single classes directory for the entire source set).

setClassesDir

abstract fun setClassesDir(classesDir: File): Unit
abstract fun setClassesDir(classesDir: Any): Unit

Sets the directory to assemble the compiled classes into.

See example at SourceSetOutput

setResourcesDir

abstract fun setResourcesDir(resourcesDir: File): Unit
abstract fun setResourcesDir(resourcesDir: Any): Unit

Sets the output directory for resources

See example at SourceSetOutput

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.

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.