What's the best way to write a task which allows i...
# plugin-development
w
What's the best way to write a task which allows its user to specify some functionality in their build.gradle? I'm working on a plugin that produces a report on a project's dependencies, but I want the user to be able to customise specific details as the report is being built. My first instinct is to allow the user to specify a function which gets invoked for each dependency as it is being processed. It seems like that function should be an input to the task, but then I run into serialisation issues, if it's not an input, but is marked as internal I worry that up-to-date checking and caching would be broken. Any advice?
a
It should work if you make your "dependency processor" serializable. E.g. you create an interface:
Copy code
interface MyDependencyProcessor : Serializable {
    // methods
}
And then user can implement it and pass instance as an input. Another option is that user passes a
Class<? extends  MyDependencyProcessor>
and you instantiate that at the execution of a task. For example https://github.com/melix/japicmp-gradle-plugin uses such strategy afaik (see filters)
w
That second option sounds like a good approach. I was trying to avoid serializable since that kind of ties the user's hands. Do you know if using
ObjectFactory.newInstance()
would cause the standard dependency injection to occur?
w
Thanks