Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn a Gradle Script Plugin into a Binary Plugin

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?

like image 468
rdmueller Avatar asked Apr 06 '26 21:04

rdmueller


1 Answers

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:

  1. Create a normal Gradle project for your favourite JVM language.
  2. In the 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.
  3. Add a compile dependency on gradleApi(). Example.
  4. Add a pluginBundle config to the build file. Example.
  5. Create a class implementing the org.gradle.api.Plugin<org.gradle.api.Project> interface. Example.
  6. Configure the plugin inside the apply method (which gives you a handle to Project). This includes adding any tasks you may need to the project.
  7. Create a class extending org.gradle.api.DefaultTask (usually) for each task. Example.
  8. Implement the task's logic in some method, annotate the method with @org.gradle.api.tasks.TaskAction. Example.
  9. Create a descriptor at 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).

like image 192
Renato Avatar answered Apr 08 '26 14:04

Renato