'ext' vs 'extensions'?

(Wujek Srujek) #1

What is the difference between ‘ext’ and ‘extensions’? The following code works fine:

class Foo { def bar() { 'fizz' } }
project.extensions.create('foo', Foo)
project.ext.xxx = 123
  println project.foo.bar() // prints 'fizz'
println project.xxx // prints '123
  println project.ext // prints org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@5c1e7bf6
println project.extensions // prints org.gradle.api.internal.plugins.DefaultConvention@5541c683

So these are distinct types, but the do the same?


(Luke Daley) #2

The ‘ext’ space is for adhoc storage. Plugins should never use this. Users use it when they just need to store some more stuff. They allow you to add properties to an object (typically primitives).

Extensions are a more formal mechanism and is how plugins should extend objects. They allow you to attach a new object to an object.

Adding an extension also adds a configure method:

project.extensions.create("foo", Foo)
  project.foo {
  somePropertyOnFoo = "bar"

There are some functional differences, but this is the conceptual difference.

Exposing methods from plugin