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:




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 "--------------------------------------------------------" [/groovy] Hope this helps anyone having the same issue.

Mick Knutson

Java, JavaEE, J2EE, WebLogic, WebSphere, JBoss, Tomcat, Oracle, Spring, Maven, Architecture, Design, Mentoring, Instructor and Agile Consulting. http://www.baselogic.com/blog/resume

View all posts

Java / JavaEE / Spring Boot Channel

BLiNC Supporters

BLiNC Adsense