Understanding ext mechanism in Gradle

satya - 3/19/2019, 11:08:59 AM

Consider this script


buildscript {      

ext {                      
 //set some variable names to be used later
 group='org.springframework.boot'
 jar="spring-boot-gradle-plugin"
 version = '1.5.14.RELEASE'
}            

repositories {       
  maven {url 'some-repo-url'}
  maven {url 'another-repo-url'}       
}             

dependencies {       
  classpath("$(group):$(jar):${version}")
}

}//eof-build-script

satya - 3/19/2019, 11:10:16 AM

Basics of Gradle first

1. All gradle code is groovy (much like java). It may not look like code but it is. Classes, methods, properties

2. The project.gradle (of which the above segment is a part) is a method body executed in the context of a Project object, whose properties and methods are available as a Java API. The above code ?configures? or ?sets up? the conventions of what a classpath is and where are the jar files. The existing code in gradle defines these objects ?repos? and ?dependencies? and expects to alter them through code. (this is how most gradle works). Touch it only when you need to and assumed conventions will do the work otherwise.

3. Each method block that looks like {} is called a closure

4. A closure has access to the local variables of an object (This is called a delgate). This object is not evident from the code. You must see documentation to see what is that object

5. the ?buildScript? code block is executed against an object called ?scriptHandler?

satya - 3/19/2019, 11:11:49 AM

What is ?ext? for ScriptHandler mean?

1. Because BuildScript code block (closure) is executed with respect to ScriptHandler I must see ?ext? as a method on the ScriptHandler object taking a closure as an argument

2. But you won?t find it

3. On digging you will see that Gradle attaches an object of type ExtraPropertiesExtension as an ?extension?. These extensions become properties. So most domain objects in Gradle: Tasks, Projects, etc. carries a hidden property called ?ext?.

4. This idea of ?extensions? is specific to Gradle and not Groovy.

5. The idea of ?extensions? allows any custom class to be attached as a property.

6. The extension class mechanism not only attaches a property but allows a closure against that property whose scope is that custom object itself.

7. So ?ext? merely makes use of that mechanism to allow its own closure

8. When ?ext? is added as an extension property to project, the ext implementation does one more thing: when a variable is set in ?ext? that variable is automatically added to the ?project? as a simple additonal property making it feasibe ?ext.a? vs ?project.ext.a?

9. As an aside groovy allows out of the box, without any extension mechanism, a closure on any property using ?SomeObject.SomeProperty.with { //code here has constrained scope of that objec }? J. ?with? is a keyword here.

satya - 3/19/2019, 11:13:48 AM

How is ext.a=?B? allowed if it is all groovy code?

1. It is common to know that ?SomeObject.property1? is same as ?SomeObject.getProperty1()?

2. However it is odd when the ?SomeObject? has no property called ?property1? and still allow it to invoke ?SomeObject.get(?property1?).

3. Groovy does. This allows for dynamic properties (associative arrays lke Javascript) on an object on the fly