Skip to content

Instantly share code, notes, and snippets.

@vgrichina
Created June 2, 2010 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrichina/423119 to your computer and use it in GitHub Desktop.
Save vgrichina/423119 to your computer and use it in GitHub Desktop.
GAnt script used to execute Groovy script inside of Grails context
import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
import org.springframework.orm.hibernate3.SessionFactoryUtils
import org.springframework.orm.hibernate3.SessionHolder
import org.springframework.transaction.support.TransactionSynchronizationManager
includeTargets << grailsScript("_GrailsBootstrap")
includeTargets << grailsScript("_GrailsRun")
includeTargets << grailsScript("_GrailsSettings")
includeTargets << grailsScript("_GrailsClean")
target('default': "Execute the specified script after starting up the application environment") {
depends(checkVersion, configureProxy, packageApp, classpath)
runScript()
}
target(runScript: "Main implementation that executes the specified script after starting up the application environment") {
parseArguments()
if (argsMap["params"].size() == 0) {
event("StatusError", ["Required script name parameter is missing"])
System.exit 1
}
compile()
loadApp()
configureApp()
configureHibernateSession()
argsMap["params"].each { scriptFile ->
executeScript(scriptFile, classLoader)
}
}
def configureHibernateSession() {
// without this you'll get a lazy initialization exception when using a many-to-many relationship
def sessionFactory = appCtx.getBean("sessionFactory")
def session = SessionFactoryUtils.getSession(sessionFactory, true)
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session))
}
def executeScript(scriptFile, classLoader) {
File script = new File(scriptFile)
if (script.exists()) {
def shell = new GroovyShell(classLoader, new Binding(ctx: appCtx, grailsApplication: grailsApp))
shell.evaluate(script.text)
} else {
event("StatusError", ["Designated script doesn't exist: $scriptFile"])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment