Using ConfigSlurper Scripts in Grails

In Java .properties files are used to configure an application. You can use similar notation to configure Grails applications as well.
A sample appName-config.groovy file that sets up the configuration items that we need.

abtesting {
amazon = new Double(0.5)
ebay = new Double(0.5)
}

A sample function in a controller that will access the configuration properties and modify them.

def abtesting = {
def propValue = grailsApplication.config.abtesting.amazon
println propValue


grailsApplication.config.abtesting.amazon = new Double(1.0)
println grailsApplication.config.abtesting.amazon
}

This code produces the following output:
>> 0.5
>> 1.0

Using Grails' JSON and HTML Builders

In order to generate JSON in groovy that can then be passed to a Grails controller you should use the JSONBuilder class. This class is part of the Grails util package. The code below demonstrates how to generate JSON and HTML.

Resulting JSON that is produced:
ResultSet {['text':'google', 'url':'http://www.google.com'], ...}

def writer = new StringWriter();
new grails.util.JSonBuilder(writer).json {
ResultSet {
links.each { link ->
Links(text: link.text, url: link.url)
}
}
}
writer.toString()

This is the code to do the same with HTML, you would use the Groovy XML MarkupBuilder instead.


StringWriter w = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(w)

builder.div (class: 'className'){
links.each { link ->
p {
b 'Title: '
builder.yield link.text
}
}
}
w.toString()
Resulting HTML that is produced:
<div class="className">
<p><b>Title: </b>google</p>
</div>

Deploying a Grails Application to Production

Beware of a weird grails config option!

To create a war file that can be deployed to a web server you would use grails war file_name.war, however this by default includes any plugins (so it seems) that you have installed with the application that you would not necessarily want deployed and can cause serious issues when deployed to your web server. So the fix is to use grails prod war file_name.war and the plugins are not included.