Monday, November 25, 2013

Workflow to follow in Grails Plugin creation process

Creating a Grails plugin is quite easy and involves few commands and configuration.
The tricky part is to handle the workflow correctly and understand properly what is going on when you use the available setup.
In any case the fist step is to create the plugin by running the following command:
grails create-plugin <plugin name>
Then there are 3 different ways to install the plugin into a Grails application:
  1. inline plugins
  2. installed from local maven cache
  3. installed from central maven repository
The order is not casual. It reflects the typical fase of the development of a plugin:
  1. when you start coding you will use it as an inline plugin. Using an inline plugin is as simple as putting a new line in main app BuildConfig.groovy file:
    grails.plugin.location."<plugin-name-no-spaces>" = "<path/to/plugin>"
    It's really simple to edit, improve and debug your plugin while you are developing in parallel or not with your main app. Just leave the plugin there and at each reload/restart the code is automatically updated. But the behavior of the plugin is not the same in all three cases: for example, plugin's exclusion is not respected using inline plugin, and also there are compilation steps that are not completely identical.
  2. so when you are ready to test your plugin you can install it in the Maven local cache. This is a feature available from Grails 2.3. Basically you have to run the following command:
    grails package-plugin
    this will create a zip file containing your plugin in one zip. Now you are ready to put it in the local Maven cache:
    grails maven-install
    now you can refer to your plugin as to any other plugin from a maven repo in BuildConfig.groovy, i.e.:
    compile 'plugin-name:0.1-SNAPSHOT'
    Why do I put '-SNAPSHOT' in the name? because Grails doesn't cache snapshot plugins, so there is no risk of installing a stale version of the plugin that is still in development somewhere else like prod env, etc..
  3. Seems that now your plugin is finished, polished and refined. Seems also that it could be useful to someone else? Did you consider to give back something to the wonderful Grails community?
    Then you can submit the plugin for approval to the manager of grails.org portal. It's quite easy and they are great, so just follow some steps:
    • create a user for the portal Grails.org
    • go to this page: Submit a Plugin.
    • fill the form in the page (plugin name, description, repository, ecc)
    • wait for the pull request of changes from one of the manager that will correct most frequent errors or adeguate your plugin to Grails standard, and accept it/improve it.
    • run the following command from the command line:
      grails publish-plugin
      Then just follow the instructions and insert your credentials for grails.org portal.

There are 2 things to consider that I've left behind. There is another way to install a plugin: package it and add the zip file as an inline plugin in your app's BuildConfig.groovy. This is almost like using the local maven cache installation, same compilation, exclusion, etc. but i don't like it because if you are at that point it means that your plugin should start acting like all other plugins, not like an inline plugin.

Please take a look at this wonderful post from Burt Beckwith.
Follow all instructions on how to reorganize the code in a plugin. If you don't do it yourself it will have to be done before the approval, resulting in time and energy waste for everyone.
Also all the information in the created <pluginName>GrailsPlugin.groovy file is important, so fill it properly!

Remember that Github is full of Grails plugins with public code that could serve as an example to you.

This post was created after i submitted my first Grails plugin, so the experience is real and up to date to Grails 2.3.3. Consider to take a look at it: Redis Flexible Cache Plugin.

There are a lot of other arguments and things to say about grails plugin, but i'll create some other posts for that.

Thanks for comments and suggestions. :)

No comments:

Post a Comment