17.1 Creating and Installing Plugins 3.0.10

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 17.1 Creating and Installing Plugins - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.0.10 ## 17.1 Creating and Installing Plugins #### Creating Plugins Creating a Grails plugin is a simple matter of running the command:
    grails create-plugin [PLUGIN NAME]
This will create a plugin project for the name you specify. For example running `grails create-plugin example` would create a new plugin project called `example`. In Grails 3.0 you should consider whether the plugin you create requires a web environment or whether the plugin can be used with other profiles. If your plugin does not require a web environment then use the "plugin" profile instead of the "web-plugin" profile:
    grails create-plugin [PLUGIN NAME] --profile=plugin
Make sure the plugin name does not contain more than one capital letter in a row, or it won't work. Camel case is fine, though. The structure of a Grails plugin is very nearly the same as a Grails application project's except that in the `src/main/groovy` directory under the plugin package structure you will find a plugin descriptor class (a class that ends in "GrailsPlugin"). Being a regular Grails project has a number of benefits in that you can immediately test your plugin by running (if the plugin targets the "web" profile):
    grails run-app
Plugin projects don't provide an index.gsp by default since most plugins don't need it. So, if you try to view the plugin running in a browser right after creating it, you will receive a page not found error. You can easily create a `grails-app/views/index.gsp` for your plugin if you'd like. The plugin descriptor name ends with the convention `GrailsPlugin` and is found in the root of the plugin project. For example:
    class ExampleGrailsPlugin {
       …
    }
All plugins must have this class under the `src/main/groovy` directory, otherwise they are not regarded as a plugin. The plugin class defines metadata about the plugin, and optionally various hooks into plugin extension points (covered shortly). You can also provide additional information about your plugin using several special properties: `title` - short one-sentence description of your plugin `grailsVersion` - The version range of Grails that the plugin supports. eg. "1.2 > *" (indicating 1.2 or higher) `author` - plugin author's name `authorEmail` - plugin author's contact e-mail `description` - full multi-line description of plugin's features `documentation` - URL of the plugin's documentation `license` - License of the plugin `issueManagement` - Issue Tracker of the plugin `scm` - Source code management location of the plugin Here is an example from the Quartz Grails plugin:

    class QuartzGrailsPlugin {
        def grailsVersion = "1.1 > *"
        def author = "Sergey Nebolsin"
        def authorEmail = "nebolsin@gmail.com"
        def title = "Quartz Plugin"
        def description = '''\
    The Quartz plugin allows your Grails application to schedule jobs\
    to be executed using a specified interval or cron expression. The\
    underlying system uses the Quartz Enterprise Job Scheduler configured\
    via Spring, but is made simpler by the coding by convention paradigm.\
    '''
        def documentation = "http://grails.org/plugin/quartz"   …
    }
#### Installing Local Plugins To make your plugin available for use in a Grails application run the `install` command:
    grails install
This will install the plugin into your local Maven cache. Then to use the plugin within an application declare a dependency on the plugin in your `build.gradle` file:
    compile "org.grails.plugins:quartz:0.1"
In Grails 2.x plugins were packaged as ZIP files, however in Grails 3.x plugins are simple JAR files that can be added to the classpath of the IDE. #### Notes on excluded Artefacts Although the create-plugin command creates certain files for you so that the plugin can be run as a Grails application, not all of these files are included when packaging a plugin. The following is a list of artefacts created, but not included by package-plugin: `grails-app/build.gradle` (although it is used to generate `dependencies.groovy`) `grails-app/conf/application.yml` (renamed to plugin.yml) `grails-app/conf/spring/resources.groovy` `grails-app/conf/logback.groovy` Everything within `/src/test/**` SCM management files within `**/.svn/**` and `**/CVS/**` #### Customizing the plugin contents When developing a plugin you may create test classes and sources that are used during the development and testing of the plugin but should not be exported to the application. To exclude test sources you need to modify the `pluginExcludes` property of the plugin descriptor AND exclude the resources inside your `build.gradle` file. For example say you have some classes under the `com.demo` package that are in your plugin source tree but should not be packaged in the application. In your plugin descriptor you should exclude these:
    // resources that should be loaded by the plugin once installed in the application
      def pluginExcludes = [
        '**/com/demo/**'
      ]
And in your `build.gradle` you should exclude the compiled classes from the JAR file:
    jar {
      exclude "com/demo/**/**"
    }
#### Inline Plugins in Grails 3.0 In Grails 2.x it was possible to specify inline plugins in `BuildConfig`, in Grails 3.x this functionality has been replaced by Gradle's multi-project build feature. To set up a multi project build create an appliation and a plugin in a parent directory:
    $ grails create-app myapp
    $ grails create-plugin myplugin
Then create a `settings.gradle` file in the parent directory specifying the location of your application and plugin:
    include 'myapp', 'myplugin'
Finally add a dependency in your application's `build.gradle` on the plugin:
    compile project(':myplugin')
Using this technique you have achieved the equivalent of inline plugins from Grails 2.x. | Quick Reference (hide)