Clarification of Gradle syntax on sourceSets script block


(Chad Davis) #1

Can someone explain this syntax to me? As I understand it, “sourceSets” is part of the Gradle DSL, correct? And it takes a configuration as it’s parameter? Full explaination of this, with callouts of which parts are Gradle DSL, which are Groovy, etc. will be much appreciated. My goal is understanding the syntax and the Gradle / Grovvy intersection; I already know what this does in terms of my build.

sourceSets {

main {

java {

srcDir ‘src/java’

}

resources {

srcDir ‘src/resources’

}

} }


(Perryn Fowler) #2

Gradle makes heavy use of the technique described here

Once you understand that you can usually follow through the gradle API docs to understand the DSL.

The starting point is explained in the Project API section of the user guide : basically that a gradle script delegates to an instance of Project

So then, if you look at Project, then you will see that it has a SourceSets method that executes the closure passed to it with an instance of SourceSetContainer as its delegate. ( The sourceSets method is actually added to Project by the Java Plugin

Here we make use of the ‘method missing’ technique described in the first link, so there is a bit of a disconnect, but it is not hard to guess that ‘main’ will result in an instance of SourceSet set as the delegate of the provided closure.

SourceSet has a method java which sets an instance of SourceDirectorySet as the delegate to its closure

and so on and so on :slight_smile:


(Chad Davis) #3

Very good answer. Thanks man.