14 July 2011

Grails and hsqldb file in war deployment

This morning I've been trying to deploy my policy and rule driven distributed components governance proof of concept prototype onto a test server (as opposed to my development machine) for a colleague to be able to play with its RESTful API. Problem was, my data is in a hsqldb file and this won't be deployed together with the war.

You need to specify where you want your DB files ("prodDb.log", "prodDb.properties" and "prodDb.script") to be stored when the war is deployed.

in DataSource.groovy:
production {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:file:./apache-tomcat-7.0.14/webapps/YieldBRGS-1/WEB-INF/lib/prodDb;shutdown=true"
}
}

Please note that in my case (apache-tomcat-7.0.14) the path is relative to the servlet container installation.

You also need to make sure your db files are copied to that location. For this, create a "_Events.groovy" script using the "create-script " command. This script should be placed in the "scripts" folder of your Grails project's root (there are other possibilities c.f. http://grails.org/doc/latest/guide/4.%20The%20Command%20Line.html#4.3%20Hooking%20into%20Events). This script will be automatically detected and triggered.

_Events.groovy
includeTargets << grailsScript("Init")
includeTargets << grailsScript("_GrailsEvents")

eventCreateWarStart = { warName, stagingDir ->
if (grailsEnv == "production") {
println "Copying hsqldb script into war"
ant.copy(file: "prodDb.script", todir: "${stagingDir}/WEB-INF/lib/")
ant.copy(file: "prodDb.properties", todir: "${stagingDir}/WEB-INF/lib/")
ant.copy(file: "prodDb.log", todir: "${stagingDir}/WEB-INF/lib/")
} else {
println "grailsEnv != \"production\""
}
}

The events are not documented so a bit of googling around is necessary. Alternatively, if you have more patience or want to know what's going on under the hood you can look for the event('...') in the Grails source code (e.g. https://github.com/grails/grails-core/blob/master/scripts/_GrailsWar.groovy).

I hope this helps!

This is the software I will use in my demonstration later this year at ServiceWave 2011 in Poznan (http://servicewave.eu/2011/). C.f. official announcement here: http://servicewave.eu/2011/demonstration-evening/