I am venturing into the world of creating my own G...
# gradle
a
I am venturing into the world of creating my own Gradle plugin, but I have two questions which I can't quite get out of the docs. 1. creating nested extensions I need to implement the ExtensionAware in my 'outer' extension right? All the docs show I can just do
outerExtension.extensions.create
but logically the
extensions
does not resolve. 2. I don't fully understand why I need ObjectFactory. Is this related to lazy instantiation of element, or something else? I fail to see why I can't just instantiate some data classes where needed.
e
you don't need to, if you create it in a way that is Gradle-decorated (e.g.
objects.newInstance
) the actual value will be a
ExtensionAware
using
ObjectFactory
, in addition to decoration, also allows Gradle to
@Inject
a
Thanks, that makes sense.
v
To make it even clearer, most things you create through Gradle are decorated. That means you can inject various things into it, and you can save a lot of boilerplate for `Property`s and similar that you should use everywhere, and they are automatically
ExtensionAware
like also most built-in DSL objects are even if they don't declare it. You can also just declare that you type extends / implements
ExtensionAware
and Gradle cares about the rest. If you do not, you can anytime just cast it, but I'd just always declare it explicitly. For extensions on extensions and so on, you also get nice type-safe accessors for Kotlin DSL automatically and so on. You can just use a data class and use
add
on
ExtensionAware
, but you really shouldn't imho, but always let Gradle decorate things and declare the
ExtensionAware
, and especially use
Property
and alike everywhere anyway. As long as you're not needing any functions on the extension it can even be an interface and Gradle does the remaining boilerplate
e
I don't think you can with
data class
because they're final
v
Can what? Create it manually and use
add
? Why shouldn't you? You can create anything you like and register it as extension, even a
Boolean
. You just shouldn't, as you then cannot lazily wire things together
e
Gradle can't decorate a final class because it involves subclassing
v
That's the point, it does not decorate if you instantiate yourself and use
add
Only if you let Gradle create the instance