Fork me on GitHub

LD.

Music, software, life… and stuff.

[ Twitter ] [ GitHub ] [ Linked In ]

Automatically packaging inline plugins in Grails 1.2

Working with module Grails applications via in-place plugins is currently not as convenient as it could be. One of the major sticking points is the need to package-plugin all of your inline plugins manually before using them in your application’s build.

If you try to build your app without packaging your in-place plugins first you will see something like…

The inplace plugin at [«plugin path»] does not have a plugin.xml. Please run the package-plugin command inside the plugin directory.

This gets tiresome real quick when you have a lot of in-place plugins. Fortunately the workaround isn’t too bad.

Add a build event listener

Add the following to scripts/_Events.groovy

eventInitInplacePluginsStart = {
    pluginSettings.inlinePluginDirectories.each { resource ->
        if (!new File(resource.file, 'plugin.xml').exists()) {
            def descriptor = pluginSettings.getDescriptorForPlugin(resource)
            generatePluginXml(descriptor.file)
        }
    }
}

That’s going to generate any missing plugin.xml’s for you… but there is a catch

Lazily load classes in the plugin descriptor

The event code above has to compile the *GrailsPlugin.groovy plugin descriptor in order to read the metadata to generate the plugin.xml. This is done before the classes in the plugin are compiled and therefore any classes that you import in your plugin descriptor are not going to be available.

The solution is to load the classes lazily via the application classloader…

class MyGrailsPlugin {

    def version = "0.1"
    def grailsVersion = "1.2.* > *"

    def doWithSpring = {
        def awesomeClass = application.classLoader("grails.plugin.my.Awesome")
        // …
    }
}

While this is slightly inconvenient, it beats manually running package-plugin in a bunch of directories.

The good news

This issue is well known about, but the fix is non trivial. It will be fixed for Grails 1.3 though.

Posted: Feb 23rd, 2010 @ 7:09 pm

Tags: #software  #grails  

Comments

Archive · RSS · Theme by Autumn