Grails “run-script” updated for Grails 1.3.5

2010/12/3

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://hg@bitbucket.org/tednaleid/grails-run-script
cd grails-run-script
grails run-script userScripts/createBook.groovy

Which shows:

Welcome to Grails 1.3.5 - http://grails.org/
Licensed under Apache Standard License 2.0
....<more grails output>...
Running script userScripts/createBook.groovy ... ...
it worked!
Script userScripts/createBook.groovy complete! ...
Application context shutting down...
Application context shutdown.

There are 6 comments in this article:

  1. 2010/12/4Tweets that mention Grails “run-script” updated for Grails 1.3.5 - Ted Naleid -- Topsy.com say:

    [...] This post was mentioned on Twitter by Ted Naleid, lucastex_grails. lucastex_grails said: #Grails “run-script” updated for Grails 1.3.5 http://goo.gl/fb/V0rfq #commandline #groovy [...]

  2. 2010/12/4Burt Beckwith say:

    Cool stuff. I’ve added this for the 1.3.6 release – thanks for the contribution.

  3. 2010/12/4tednaleid say:

    @Burt – excellent! Thanks for letting me know, I’ve updated the post above with a note that it’ll be in 1.3.6 and above.

  4. 2011/01/4Fabien7474 say:

    Very cool indeed. Thank you very much for this. I will definitely use it in production.

  5. 2011/01/13Grails Interview Questions ( now with some Answers! ) | Tomás Lin’s Programming Brain Dump say:

    [...] change like the console does, so it is useful also for long-running scripts, although the new run-script command in Grails 1.3.6 might be better suited for [...]

  6. 2012/03/28valerius say:

    Is it possible to run a script from a deployed war? Basically, instead of requiring the source, once an application is deployed to the server, is it possible to run a script using the war file only (because the script could be included in the war itself?) The reason I ask if it would be nice to have reporting cron/thread running in the app in production, but separately run it from the command line without requiring the source code be on the server, etc. Thx!

Write a comment: