Adding a new Script
A plugin can add a new script simply by providing the relevant Gant script within the scripts directory of the plugin:
+ MyPlugin.groovy
+ scripts <-- additional scripts here
+ grails-app
+ controllers
+ services
+ etc.
+ lib
Adding a new Controller, Tag Library or Service
A plugin can add a new controller, tag libraries, service or whatever by simply creating the relevant file within the
grails-app
tree. Note that when the plugin is installed it will be loaded from where it is installed and not copied into the main application tree.
+ ExamplePlugin.groovy
+ scripts
+ grails-app
+ controllers <-- additional controllers here
+ services <-- additional services here
+ etc. <-- additional XXX here
+ lib
Providing Views, Templates and View resolution
When a plug-in provides a controller it may also provide default views to be rendered. This is an excellent way to modularize your application through plugins. The way it works is that Grails' view resolution mechanism will first look the view in the application it is installed into and if that fails will attempt to look for the view within the plug-in.
For example given a
AmazonGrailsPlugin
plug-n provided controller called
BookController
if the action being executed is
list
, Grails will first look for a view called
grails-app/views/book/list.gsp
then if that fails will look for the same view relative to the plug-in.
Note however that if the view uses templates that are also provided by the plugin then the following syntax may be necessary:
<g:render template="fooTemplate" plugin="amazon"/>
Note the usage of the
plugin
attribute, which contains the name of the plugin where the template resides. If this is not specified then Grails will look for the template relative to the application.
Excluded Artefacts
Note that by default, when packaging a plug-in, Grails will excludes the following files from the packaged plug-in:
- grails-app/conf/DataSource.groovy
- grails-app/conf/UrlMappings.groovy
- Everything under web-app/WEB-INF
If your plug-in does require files under the
web-app/WEB-INF
directory it is recommended that you modify the plug-in's
scripts/_Install.groovy
Gant script to install these artefacts into the target project's directory tree.
In addition, the default
UrlMappings.groovy
file is excluded to avoid naming conflicts, however you are free to add a UrlMappings definition under a different name which
will be included. For example a file called
grails-app/conf/BlogUrlMappings.groovy
is fine.
Additionally the list of includes is extensible via the
pluginExcludes
property:
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
This is useful, for example, if you want to include demo or test resources in the plugin repository, but not include them in the final distribution.