Starting with it’s initial version a couple of years ago, Grails comes with a very nice predefined project structure, e.g. domain classes go into
grails-app/domain. Every artefact has its well defined location. Almost every Grails application doing a little more than a simple “Hello world” will use at some extend one or more of the 500+ available plugins. The easiest way is to add plugins by using
grails install-plugin <pluginname>
The plugin gets downloaded from the central repository and added into your application, either in
application.properties or into
grails-app/conf/BuildConfig.groovy (default for Grails 2.0). In both cases the application contains only a reference to the installed version number.
This approach works very nice as long as you don’t have to change anything inside the plugin. I’ve had multiple times the necessity to modify external plugins, due to fixing bugs or due to some special requirements that are not yet covered by the existing released version. To deal with this, there are a couple of approaches:
1) most naive:
The most simple thing to do is just opening the plugin’s source files using your IDE and make your modifications. This has some real downsides:
- your changes will be lost whenever you install a new upstream version of the plugin
- since Grails unpacks installed plugins by default into
$HOME/.grails/<grailsversion>/projects/bullseye2/plugins/<pluginname>, your changes are not part of any SCM and only reside locally.
I’ve warned you! Don’t do this!
2) build customized plugins in a seperated location
- download the sources of the desired plugin, unpack it outside your main application
- modify the plugins
- create ‘your’ version:
- switch to your application and install the generated plugin:
grails install-plugin <zipfile>
This approach annoys me, since you need to repeat the ‘package-plugin’/'install-plugin’ cycle for each an every change in the plugin. So inline-plugins to the rescue…
3) use inline plugins and copy the plugin’s sources into your application’s repo
Grails has the ability to use ‘inline plugin’. There are a couple of nice blog posts covering this, so I’m just using Burt Beckwith’s slides as a reference. You basically unzip the plugin’s zip artefact into some folder inside your application (e.g. plugins folder), reference it in
grails-app/conf/BuildConfig.groovy and add the extracted files to your application’s repo.
In that case you’ve basically created a diverging copy of the plugin. Whenever there’s a new release of the plugin, integrating this back into your app might become a pita. You need to reapply your changes on top of the version of the plugin. That’s doable, but requires some advanced git technique, is error prone and can consume a lot of time – I know what I’m talking about here, trust me! Another major downside is that your local fixes and improvements are not easy to contribute back to upstream.
4) use inline plugins with git submodule
At that point, I’m expecting that you’re already using git for your project. If not, NOW is the time to do this and familiarize yourself with git. It’s worth it, promised!
- create a distinct folder inside your project, e.g. ‘plugins’ that will contain all customized plugins
- Find out the location of the plugin’s scm. A lot of plugins host their sources now on github. If so, fork this repository on github. If it’s using svn, you could mirror it to github, see a nice blog post for this. Basically you have now a forked plugin available on github.
- clone the forded repo into your project, I’m using the spring-security-ui plugin here as example:
$ git submodule add git://github.com/sarmbruster/grails-spring-security-ui.git plugins/grails-spring-security-ui
Cloning into plugins/grails-spring-security-ui...
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
# new file: .gitmodules
# new file: plugins/grails-spring-security-ui
git add .gitmodules plugins/
git commit -m "added submodule"
The nice thing is that your application’s repo contains now a reference to the plugin’s repo and it even remembers the sha-id of the plugin you’re currently using.
- now add plugins/grails-spring-security-ui as a inline plugin by adding to
- push your changes
- last but not least ask your collaborators on the project to pull your changes, let them do
git submodule update --init. This command is only required once for each working copy.
The really nice thing about this setup is that you can easily share your plugin changes to the upstream repository by a pullrequest. And the other way is also pretty easy: when the upstream author accepts your pullrequest and/or adds a some new functionality you can directly consume this by going to your plugin’s directory and use
$ git fetch upstream
$ git merge upstream/master
NB: this results in a to-be-committed change in the upstream repo since the sha-id of the referenced repo has changed.
- whenever you commit something to a submodule the parent repo will have a non-empty status since your copy now references another another sha-id.
- choose the URL of the custom plugin repo carefully. If you’re the only developer make changes in the plugin, you might use the repo’s public address and override locally the push url to your private URL by
$ git remote set-url --push email@example.com:sarmbruster/grails-spring-security-ui.git
Now there’s a difference in fetch and push URLs:
$ git remote -v
origin git://github.com/sarmbruster/grails-spring-security-ui.git (fetch)
origin firstname.lastname@example.org:sarmbruster/grails-spring-security-ui.git (push)
Your collaborators will only get the public fetch URL for both and won’t be able to commit to your private repo (that’s why we’re calling it private, right?).
The other scenario is when you want multiple developers commit to the plugin repo. In this case you need to grant them access to your private repo (or even set up a github organization for that) and use the private URL in the ‘git submodule add’ command.
Contributing your changes back to the plugin upstream repo is very well explained at Fork a repo.
With the approach explained you’re able to customize any plugin and track the plugin changes from your applications repo. Contributing your bugfixes and improvements back to the upstream author is a piece of cake now as well as benefiting from upstream changes.
I’d be greedly waiting for your thoughts and for discussion on this setup. Combining this with git-flow looks like a kind of best practice for Grails projects – at least for me.