tags:

views:

80

answers:

2

Hi,

I need to write a script for a Grails application that modifies persistent data, send e-mails to users, etc.

While there's nothing Grails-specific about this - I could accomplish these tasks using JDBC, JavaMail - I'm hoping there's a better way. Specifically, if I could write a Groovy script which has access to the Spring beans, and domain classes defined within the Grails application, it seems like I could save myself a lot of time.

Any suggestions?

  • Don
+1  A: 

Based on your question I'm assuming this a one time script you need to run, rather than some functionality that belongs in the grails app. For this, you could write a groovy script and run it in the grails console:

grails prod console

In the console you would have access to the Domain classes (and GORM). You could then dump the mail/activation/commons-email jars in the lib directory or install the grails Mail plugin.

John Wagenleitner
+1  A: 

Another possibility is to create a gant script. For example a HelloWorld.groovy script can be created with:

grails create-script hello-world

Within this script you have access to the spring context via the appCtx variable and the GrailsApplication instance via the grailsApp variable. You can use the latter to load the User domain classes with

def artistClass = grailsApp.classLoader.loadClass("com.example.User")

and you can then invoke GORM methods in the usual way:

artistClass.list()
Don