Writing custom gradle plugins for my company’s project builds I have come across the same issue several times now and never found an answer searching:
I have a plugin that adds stuff to the project: + an extension + an instance of a custom task
The custom task is defined in a dedicated class. It’s built to be reusable. For use in my plugin I want to set some of its properties based on how the extension is configured. Unfortunately the task initialization closure gets executed with applyPlugin(), which is before the extension could be configured by the user.
What is a good (concise, readable) way to parameterize the task with the extension’s properties?
Then you rewrite your custom task to receive a closure in it’s setter method and resolve the closure in the execution phase, i.e. the @TaskAction method.
Since it is private, creating a new TaskAction and calling it is not possible. The execAction object is private as well, so the method is not able to just be rewritten in the extending class.
I’m not exactly sure how the solution posted above helps me since I can’t put additional code in the TaskAction and still execute the intended code without reimplementing large parts of the Exec class.
Most Gradle tasks aren’t designed to be subclassed. Why do you want to subclass ‘Exec’? Do you really want your task to expose all of ‘Exec’'s properties, or do you just want to make your task execute something? In the latter case, you should use ‘Project.exec’.
I didn’t realize Project.exec existed and that certainly seems like it would be an easier alternative to what I was trying to do. Thanks for the pointer.