0

I am struggling to understand the gradle groovy syntax for dependencies and what is going on behind the scenes. As a starter I don't see what is exactly happening in this code snippet ....

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
}

What I (hope to) understand (please correct if I am wrong):

  • dependecies is a method of the org.gradle.api.Project interface / org.gradle.api.internal.project.DefaultProject class which expects a Closure to configure the dependencies of the project.
  • compile is a org.gradle.api.artifacts.Configuration which has been added by the org.gradle.api.plugins.JavaPlugin

What I don't understand:

  • What exactly is happening by specifying group: 'commons-collections', name: 'commons-collections', version: '3.2' ?

  • Does this invoke some magic method of the compile configuration object (if so, which one)?

  • Are group, name and version named parameters of a method call or are they method calls themselves?

  • Does this create a new org.gradle.api.artifacts.Dependency instance which is added to the compile configuration?

Ueli Hofstetter
  • 2,409
  • 4
  • 29
  • 52

1 Answers1

3

Gradle (like other tools built with Groovy) makes lots of use of methodMissing(...): http://www.groovy-lang.org/metaprogramming.html#_methodmissing

So what happens in the case of dependencies is that you invoke a method that does not exist. The method name is the name of the configuration, and its arguments are the dependency specification.

methodMissing(...) will be called and this will in turn call one of the add(...) methods of DependencyHandler: https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html

Johan Stuyts
  • 3,607
  • 1
  • 18
  • 21