Grails "Run-script" Updated for Grails 1.3.5

| Comments

UPDATE: This script has been merged into the Grails 1.3.6 release so it’ll be part of core Grails with the next version of grails. You’ll only need to get it manually for versions earlier than that.

A while ago, I created a grails script that allowed the execution of a groovy script within the grails context. Using this gant script gives you access to all of the grails domain objects, hibernate session, and other plugins that you have installed into the app, all from the command line.

It had rotted a bit in the grails 1.3 days (I’d last updated it for grails 1.2), but finally got around to updating it to work with the latest version of grails (currently 1.3.5).

I’ve created a mercurial repository to hold a test project for the script so that it’s easy for me to verify that it works with the latest version of grails.

If you don’t have mercurial installed, you can just get the raw version of the file and save it in the “scripts” directory of your grails application.

Then you can create a groovy script that you’d like to execute (often to bootstrap data, or run some periodic process) and execute it with:

grails run-script path-to-script/scriptName.groovy

I often create my groovy scripts within a “grails console” and get them to work there, then save them for later execution by “run-script”.

You can see it in action by cloning the sample project out on bitbucket and executing the sample script I have in it:

hg clone ssh://
cd grails-run-script
grails run-script userScripts/createBook.groovy

Which shows:

Welcome to Grails 1.3.5 -
Licensed under Apache Standard License 2.0
Running script userScripts/createBook.groovy ... ...
it worked!
Script userScripts/createBook.groovy complete! ...
Application context shutting down...
Application context shutdown.