Chapter 8. Plugins

Plugins are a great way to add functionality to a Grails application. They’re structurally nearly identical to an application, so they can add any of the artifacts that an application can (domain classes, services, etc.) and even create new artifact types. They can also contribute static resources (e.g., JavaScript or CSS files) and Gant scripts. And there are startup hooks that let plugins register Spring beans, add elements to the web.xml file, add dynamic MetaClass methods and properties, and listen for and respond to file modifications during development.

Plugins can also be used to modularize an application. Ordinarily, when thinking of plugins, you probably think about the hundreds of plugins that have been released to the central Grails plugin repository. These were created by community members and Grails team members to be reused by anyone who is interested, but you can create private plugins and even host your own plugin repository. If you find that you have common code that you want to share between applications, you can extract it out into a plugin and install it in all the applications that use it.

Creating a Plugin

Creating a new Grails plugin is very similar to creating a Grails application, but instead of running grails create-app <appname>, you run grails create-plugin <pluginname> (or use your IDE). The directory structure of an application and a plugin are very similar (Figure 8-1). This is intentional, because a plugin can contribute any of the ...

Get Programming Grails now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.