I have a bunch of gradle script plugins like this one https://github.com/docToolchain/docToolchain/blob/master/scripts/exportExcel.gradle and would like to turn them into "real" binary plugins in order to easily distribute them.
I am aware that I can reference the script plugins form a build via http, but that is not as nice as a real plugin.
As I can see, one way to write a plugin is to implement the org.gradle.api.Plugin interface. This basically means I would have to rewrite all mit script plugins.
Is there another way to easily turn them into binary plugins? Maybe some kind of wrapper?
Your script seems to be easily convertible to a plugin.
Why do you say you would have to rewrite your scripts? It's basically a matter of chaging some small details and adding all the boilerplate/metadata, as far as I can tell.
I've written quite a few plugins, here's a really simple one: https://github.com/renatoathaydes/pony-gradle-plugin
This is what you need to change to make a real plugin:
buildScript block, add a classpath dependency on com.gradle.publish:plugin-publish-plugin:0.9.10 and apply the com.gradle.plugin-publish plugin. Example.gradleApi(). Example.pluginBundle config to the build file. Example.org.gradle.api.Plugin<org.gradle.api.Project> interface. Example.apply method (which gives you a handle to Project). This includes adding any tasks you may need to the project.org.gradle.api.DefaultTask (usually) for each task. Example.@org.gradle.api.tasks.TaskAction. Example.META-INF/gradle-plugins/plugin-name.properties with an entry like implementation-class=your.plugin.Class. Example.That's basically it!
You might probably want to make the plugin configurable, the Gradle docs show how to do that, but here's their current example for convenience:
class GreetingPluginExtension {
String message = 'Hello from GreetingPlugin'
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
def extension = project.extensions.create('greeting', GreetingPluginExtension)
// Add a task that uses configuration from the extension object
project.task('hello') {
doLast {
println extension.message
}
}
}
}
Which then can be used inside the user's build file like this:
greeting {
message = 'Hi'
}
To actually publish the plugin in the Gradle Portal, you need to create an account... see this for more details. (Or you can just publish it on Bintray, but then it's a little more awkward for your users to apply your plugin - on the other hand Bintray is much better as a repository, so I end up doing both, see the examples above).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With