I'm trying to understand deeply how gradle is working.
Let's take the case of a basic dependencies {}
declaration in a build.gradle
script.
dependencies
is a method on Project object.
DSL Project object = org.gradle.api.Project interface:
void dependencies(Closure configureClosure)
Configures the dependencies for this project.
This method executes the given closure against the DependencyHandler for this project. The
DependencyHandler is passed to the closure as the closure's delegate.
So: method receives as parameter a configuration closure and establish that closure's delegate object is DependencyHandler
class, than execute the closure against it's delegate object.
Having this example:
dependencies {
// configurationName dependencyNotation
implementation 'commons-lang:commons-lang:2.6'
}
This is translated in a call of method add
of DependencyHandler
class:
Dependency add(String configurationName, Object dependencyNotation) Adds a dependency to the given configuration.
And now the question:
How exactly is done the translation of the line
implementation 'commons-lang:commons-lang:2.6'
into a class method call (e.g. DependencyHandler.add()
) and who is responsable ?
My impression is that there is a missing explanation in the documentation, something like: default method on this delegate object DependencyHandler
is add(...)
, so each configClosure's line, if matching notation configurationName dependencyNotation
, will be translate into delegate's object default method.
But this is just a possible interpretation.
Ideea is: I give a closure with multiple lines, this is executed agains a delegation object which happens to have methods add()
, and magically for each line this method is called ... how is this happening, based on what mechanism ? Is the Project.dependencies()
method doing this, is the delegate object itself, or some other groovy specific mechanisms, etc.
Thank you.