Introduction to Grails plugins Grails Plugins Tutorials Understanding Grails Plugins

What is Grails Plugins

What is a plugin

One of the most important factors which makes Grails very configurable is the concept of plug and play plugins. Almost any functionality which needs to be coded in a web application can be introduced via plugins without any code to be written. The beauty of plugin lies in, we donot have to write any code but just include the plugin and customize codes to our requirement.

A plugin is nothing but a regular Grails application with this special file; but when plugin is installed, the plugin structure slightly differs.

Plugin’s structure

When a plugin is installed into a project, the contents of grails-app directory goes into a directory like $PROJECT_HOME/plugins/example-1.0/grails-app . They will NOT be copied into the main source tree. A plugin doesnot tamper a project’s primary source tree.

The plugin directory structure appears as below :

+ grails-app
     + controllers
     + domain
     + taglib
     etc.
 + lib
 + src
     + java
     + groovy
 + web-app
     + js
     + css

The static resources such as those inside the web-app directory will be copied into the project’s web-app directory under a special “plugins” directory for example web-app/plugins/example-1.0/js . It is therefore the responsibility of the plugin to make sure that it references static resources from the correct place. For example if you were

<g:createLinkTo dir="/plugins/example/js" file="mycode.js" />

To make this easier there is a special pluginContextPath variable available that changes whether you’re executing the plugin standalone or whether you’ve installed it into an application:

<g:createLinkTo dir="${pluginContextPath}" file="js/mycode.js" />

At runtime this will either evaluate to /js or /plugins/example/js depending on whether the plugin is running standalone or has been installed in an application.

Java & Groovy code that the plugin provides within the lib and src/java and src/groovy directories will be compiled into the main project’s web-app/WEB-INF/classes directory so that they are made available at runtime.

What a Plugin can and can’t do

A plugin can do just about anything, but a good plugin should be as intrusive as possible. It should use convention over configuration where possible, otherwise provide an artifact in a project’s grails-app/conf directory where configuration can be done.

One thing a plugin cannot do though is modify the web-app/WEB-INF/web.xml or web-app/WEB-INF/applicationContext.xml files. A plugin can participate in web.xml generation, but not modify the file or provide a replacement. A plugin can NEVER change the applicationContext.xml file, but can provide runtime bean definitions .

Number of View :1173

Tags:

  1. No comments yet.

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache