I want to customize the wrapper task by generating an additional script that will set some environment variables, install packages (java version for the build) etc. prior to the build starting. To do this I made a class that extends the org.gradle.api.tasks.wrapper.Wrapper class. To use this class I create a task as follows:
// configure MyWrapper class here
}
However when I call super.generate() (the method annotated with the TaskAction annotation in the super class) an exception is thrown due to the method having default visibility.
I see two solutions moving forward:
Make a pull request to change the visibility of the generate method in the Wrapper class to protected
More preferably find the method in the super class that is annotated by the TaskAction and call that.
What are your recommendations on how to proceed. If possible we would like to do this without writing a plugin as a Task class is all that is necessary to accomplish what we want.
In practice, folks generally just make the modifications to the scripts and check them into VCS. The wrapper scripts themselves very rarely change so there’s almost never a need to regenerate them, even when upgrading Gradle versions.
I guess that the main benefit of wrapping this in a class is that if we want to make these modifications on a large scale, across many projects, we don’t have to edit each script by hand or copy and paste, we can just run our modified wrapper class.
That’s understandable. In general, most Gradle tasks aren’t really designed for extensibility in the OO sense. If you need to modify the scripts generated by the Wrapper task I’d probably just implement that as a separate task that depends on the ‘wrapper’ task or as a doLast { } on the ‘wrapper’ task itself.