Hello everyone, what’s the recommended way to writ...
# plugin-development
a
Hello everyone, what’s the recommended way to write DSL elements, say sections of an extension, that behave properly with the configuration cache? I have the need to grab some properties from the
project
instance (such as name, description, etc) and other properties defined in
gradle.properties
for example. As I understand values should be sources from providers, as well as avoid keeping references to “unsactioned” things such as the
project
instance and non-serializable types. Is it OK to pass the
project
instance to the constructor (using
@Inject
of course) and derive all needed providers from there? Or should I pass all required providers as constructor args? TIA
m
Almost all my extensions have a
Project
property. I don't even inject it, I pass it explicitely. No problems with CC so far
a
I see. I thought we’re encouraged to define DSL objects as a pair of interface/class types and use a factory method from
ObjectFactory
to have managed/injected args work. Of course one can directly instantiate an extension type and pass args to its constructor, skipping the interface as well. It's hard to keep up with idioms.
m
I use
project.extensions.create
(doc). You can pass arguments there
So yea, you can use Gradle injection if you want to. But you can also instantiate everything manually.
1
I used to instantiate manually because it's a good way to understand the magic. Now that I get it, I let Gradle decorate the classes because I'm lazy. Both work
Part of me wants to benchmark the cost of decorating all those Java classes. This must be more expensive than just calling plain constructors.
👀 1
t
I also use project.extensions.create and pass the project instance into an Inject annotated constructor. It's ok to do this for CC. The primary constraint is to not attempt to use that project instance in task execution, and also don't try to access it from another project
1
a
Gotcha. Thanks