Hello. New plugin developer here. I am writing a p...
# plugin-development
a
Hello. New plugin developer here. I am writing a plugin that produces two or more tasks for consumers of the plugin to invoke. Each of the tasks, when run, has a dependency on an instance of a class. Instantiation of the class is somewhat "expensive" and the same instance can be used/shared between each of the tasks that were provided. My question to you all is: is there anything special I need to do or watch out for here? Can I simply use the traditional Singleton pattern on my class and expect things to work just fine in gradle?
v
Heavily depends. With "traditional singleton" I guess you mean some static state like a static instance. Then it depends on whether the class is just costly to initialize, or whether it also holds state. If it holds state, it is a very bad idea to use static stuff, because it will outlive the build execution in the running Gradle daemon and thus influence later builds running on that agent, potentially even builds of other projects also using your plugin.
You should most probably use a shared build service instead. Those are build-scoped, so you can use it from both of your tasks and after the build it is gone.
👍 1