Building WAR files

Grails docs go a long way towards being the only materials you need to learn how to use Grails. Unfortunately, it can miss a few things that are common use cases.

In any real world production app, you don’t manage your configuration from within your WAR file. How do you have a configuration file deployed out to your production server beside your WAR file? Most of the posts out there about external configuration all talk about hard coding external config locations into a variable like so:

grails.config.locations = [ “file:${userHome}/${appName}-conf.properties”] Well, that sucks. That is assuming, again, more about production then you really can afford. The configuration of the production system, isn’t up to the developer. It’s up to the Release Manager or System Admin or a whole team of people besides you. Rather than force them into accommodating you, the programmer, you need to make it easy for them to specify WHATEVER THE FUCK THEY WANT.

So here it is, place the following in Config.groovy:

def ENV_NAME = "APPNAME_CONFIG"
if(!grails.config.location || !(grails.config.location instanceof List)) {
    grails.config.location = []
}
if(System.getenv(ENV_NAME)) {
    println "Including configuration file specified in environment: " + System.getenv(ENV_NAME);
    grails.config.location << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
    println "Including configuration file specified on command line: " + System.getProperty(ENV_NAME);
    grails.config.location << "file:" + System.getProperty(ENV_NAME)
} else {
    println "No external configuration file defined."
}

The key points in this method that are not in the docs or blogs to come before me: It actually works. It appears some devs don’t bother testing their methods before posting the code. You have to initialize the grails.config.location as a list if it isn’t already. And it is grails.config.location. Not, location[s].

Source