Tuesday 26 July 2011

Grails Accessing Raw XML from a Request

There have been times in a grails controller I've wanted direct access to the xml in a POST request. You can retreive this via the request object: request.reader.text But what if you need access the raw xml and then to also use the xmlslurper to pick apart the xml. Accessing the request.XML after request.reader.xml throws this exception: java.lang.IllegalStateException: getReader() has already been called for this request

Say I have the following xml:


and I want to access the raw xml tag 'three' but I want programmatic access to the tag 'two'. The following allows that:

Monday 18 July 2011

Grails Script to Copy Jar Dependencies

The usual way to include jar dependencies in Grails is to set the versioned dependencies in the BuildConfig.groovy. One issue I can see with this is that it forces you to use Maven, Ivy or at the very least a directory structure that is versioned. While this is a good practice and should be encouraged, there are times when you have to work with legacy build systems and you dont have the benefit of good structure. A script such as the following can copy the jars into the lib directory of the grails project. Call it something like Copyjars.groovy and put it in the scripts directory in the root of the grails project.





Then run the script using "grails prod copyjars"

Friday 15 July 2011

Grails Jar'd Configuration Property Files

Say you have an existing Java project which has a plethora of configuration property files and you want to use some of these properties in your new Grails project. How can you do this? One approach is to:
  1. Put all your property files into a jar and place the jar in the 'lib' directory of your grails project
  2. Modify the Config.groovy and add the line -
    grails.config.locations = [ "classpath:file1.properties", "classpath:file2.properties" ]
  3. Then depending where you are in the app you have access to the property variables and can dereference them e.g. ${my.file1.prop}