2

I am experimenting with some gradle at a new project, and in its settings.gradle, file I see these few lines that I am unable to make sense of as to what groovy structure or a language feature it is and what it does and how it works:

plugins {
  id "com.gradle.build-scan" version "1.12.1"
  id "cz.malohlava"     version "1.0.3"
}

buildScan {
  server = "some.host.com"
  publishAlways()
}

I was suspecting it was either a a closure or an interface of some sort, but could not make head or tail of it.

Any help in understanding following will be a great help:

  • what it does?
  • How plugins and buildScan works here from the language's perspective?
Anton Hlinisty
  • 1,441
  • 1
  • 20
  • 35
triangorithm
  • 111
  • 9
  • There are some existing questions which may help with understanding, i.e. https://stackoverflow.com/questions/32131860/how-gradle-dependencies-work – tim_yates Apr 14 '18 at 10:30

2 Answers2

3

From the language perspective, the closures are executed in the context of another objects than the build script. This is called delegation in Groovy.

http://groovy-lang.org/closures.html#_delegation_strategy

plugin delegates to https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html

buildScan delegates to Build Scan Plugin's extension object which configures the plugin.

musketyr
  • 808
  • 5
  • 16
2

There may be some trickery here that I don't understand, particularly as I can't find either plugins() or buildScan() in the API docs. Nonetheless, the following is a reasonable reading of what the syntax means.

  1. plugins {} and buildScan {} are both methods that take a closure (see other answers for explanation of this) as an argument.

  2. Each closure has a delegate object of a particular type that's different depending on the method using the closure, i.e. the delegate of plugins() will be of a different type to the delegate of buildScan()

  3. Within the closure, unqualified methods and properties will be executed against the delegate object. So for the plugins {} block, id(...).version(...) will be called against its delegate. For buildScan {}, you're setting the property server on the delegate and calling its publishAlways() method.

Honestly, I don't know how useful the above information is for using and understanding Gradle, but I think it's what you're asking for. Hope it helps!

Peter Ledbrook
  • 4,302
  • 1
  • 21
  • 18