Configurations and closures

(Ryan Nelson) #1

This may be a Groovy question, but I discovered it in the context of Gradle, so I’m wondering if someone can help me understand. Given this single Gradle script:

apply plugin: 'java'
  configurations.runtime ( { println "hello" } )

Gradle will output “hello” when executed. Why? The Configuration interface has no methods that accept a closure. Can someone explain to me how this works?

(Peter Niederwieser) #2

‘configurations’ is an instance of ‘ConfigurationHandler’ and uses Groovy meta-programming to allow you to say things like ‘configurations.runtime { extendsFrom compile }’. In other words, ‘configurations’ provides dynamic methods that accept a closure argument.

(Ryan Nelson) #3

Fascinating; thanks, Peter. Now, I looked for the ConfigurationHandler class in the source–I found it it some old copies, but not in the latest tree. Can you point me to where the Configuration class gets wrapped? I’d like to understand this further.

(Peter Niederwieser) #4

Sorry it’s ‘ConfigurationContainer’, not ‘ConfigurationHandler’. Dynamic methods with closure argument are handled by ‘DefaultNamedDomainObjectCollection.invokeMethod()’.

(Ryan Nelson) #5

Great! Thanks.