Getting Grails external configuration working in the real world.

I have gone through several books and countless Blogs to try to get a valid working WAR that would access my external configuration files, both .properties and .groovy files.

First, I have read through the online Grails documentation, ‘Grails In Action’, as well as the following Blogs:

http://www.comitservices.com/wp/?p=133

http://phatness.com/2010/03/how-to-externalize-your-grails-configuration/

http://blog.zmok.net/articles/2009/04/22/playing-with-grails-application-configuration

The online docs did not work at all for me.

The blogs all seemed straight forward, but again, they did not fully work for me.

I wanted a way that  external configuration could be specified in order:

  1. From Command Line
  2. System Environment Property
  3. $USER_HOME\.grails

I thought this would be easy, but I actually spent the better part of 6 hours to get this correct.

Here is what I ended up with:

def ENV_NAME = "${appName}.config.location"
if (!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []
}
println "--------------------------------------------------------"
// 1: A command line option should override everything.
// Test by running:
// grails -Ddivr.config.location=C:\temp\divr-config.groovy run-app
// or
// grails -Ddivr.config.location=C:\temp\divr-config.properties run-app
if (System.getProperty(ENV_NAME) && new File(System.getProperty(ENV_NAME)).exists()) {
    println "Including configuration file specified on command line: " + System.getProperty(ENV_NAME)
    grails.config.locations << "file:" + System.getProperty(ENV_NAME)
}
// 2: If this is a developer machine, then they will have their own config and
//    I should use that.
else if (new File("${userHome}/.grails/${appName}-config.groovy").exists()) {
        println "*** User defined config: file:${userHome}/.grails/${appName}-config.groovy. ***"
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
}
// 3: Most QA and PROD machines should define a System Environment variable
//    that will define where we should look.
else if (System.getenv(ENV_NAME) && new File(System.getenv(ENV_NAME)).exists()) {
    println("Including System Environment configuration file: " + System.getenv(ENV_NAME))
    grails.config.locations << "file:" + System.getenv(ENV_NAME)
}
// 4: Last resort is looking for a properties based configuration on the developer
//    machine.
else if (new File("${userHome}/.grails/${appName}-config.properties").exists()) {
        println "*** User defined config: file:${userHome}/.grails/${appName}-config.properties. ***"
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config.properties"]
}
// 5: Houston, we have a problem!
else {
        println "*** No external configuration file defined. ***"
        println "*** No external configuration file defined. ***"
        println "*** No external configuration file defined. ***"
}

println "(*) grails.config.locations = ${grails.config.locations}"
println "--------------------------------------------------------"

Hope this helps anyone having the same issue.

VN:F [1.9.1_1087]
Rating: 9.0/10 (4 votes cast)
VN:F [1.9.1_1087]
Rating: +1 (from 1 vote)
Getting Grails external configuration working in the real world., 9.0 out of 10 based on 4 ratings
  • Share/Bookmark

This entry was posted on Thursday, April 1st, 2010 at 8:03 am and is filed under Grails-Groovy, Java-JavaEE-J2EE. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “Getting Grails external configuration working in the real world.”

  1. Tweets that mention Getting Grails external configuration working in the real world. | BASE Logic, Inc. -- Topsy.com Says:

    [...] This post was mentioned on Twitter by Mick Knutson. Mick Knutson said: Getting Grails external configuration working in the real world.: I have gone through several books and countless … http://bit.ly/bZydsu [...]

  2. links for 2010-06-01 | andy.edmonds.be Says:

    [...] Getting Grails external configuration working in the real world. | BASE Logic, Inc. (tags: grails configuration external) [...]

  3. blackorangebox Says:

    Thanks for the real-life configuration!

    This code should reside in Config.groovy, right?

    VN:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
  4. blackorangebox Says:

    Works perfectly! Thanks=)

    VN:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
  5. Blog bookmarks 08/11/2010 « My Diigo bookmarks Says:

    [...] Getting Grails external configuration working in the real world. | BASE Logic, Inc. [...]

Leave a Reply

You must be logged in to post a comment.