SpringOne 2GX 2011

Chicago, October 25-28, 2011

Grails user-specific configurations

Posted by: Colin Harrington on 2009-11-26 10:15:00.0

grailsI asked a question on the GUM (Groovy Users of Minnesota) User list about how to achieve a user/machine specific config.

I didn't have much time to figure it out but this is what I ended up finding out:

Grails 1.1.1 create-app generates a Config.groovy with this as the first few lines that would have told me what I need to know if I actually took the time to read it:
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
//
// grails.config.locations = [ "classpath:${appName}-config.properties",
//                             "classpath:${appName}-config.groovy",
//                             "file:${userHome}/.grails/${appName}-config.properties",
//                             "file:${userHome}/.grails/${appName}-config.groovy"]
//
// if(System.properties["${appName}.config.location"]) {
//    grails.config.locations << "file:" + System.properties["${appName}.config.location"]
// }

So in my case all I had to do was put this in Config.groovy:
if (new File("${userHome}/.grails/${appName}-config.groovy").exists()){
    grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
}

which allowed me to override properties by doing something like this in my ~/.grails/${appName}-config.groovy :
username = "sa"
password = "sekret"
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // use your imagination...
        }
    }
}

I also ran across a little gem that you can do the same type of config merging with your BuildConfig.groovy by implementing a ~/.grails/settings.groovy file. (yes its hardcoded rather than a config.locations property in BuildConfig.groovy – see BuildSettings.groovy for more)

Now I really wish there was a way to set defaults for things like server.port in the configs.

Thanks, Ted Naleid, Scott Vlaminck, and Robert Fischer for helping me find what I was looking for!



About Colin Harrington

Colin Harrington

Colin Harrington is a Senior Consultant at Object Partners, Inc with over 3.5 years of Grails experience. Based in Minnesota, Colin has 10+ years of experience developing web-based applications.

Colin is an agile practitioner and has been a key component of many powerful fast-paced local and remote teams with varying levels of composition and ability. He is a Technical leader and a forward thinker with a knack for delivering potent and engaging web-applications.

Colin is an active member of the Groovy and Grails community and an active speaker at local user groups, events and conferences such as MinneBar, GUM, GR8Conf in the US, etc.

More About Colin »

NFJS, the Magazine

2011-10-01 00:00:00.0 Issue Now Available
  • Plugging into Gradle Plugins

    by Tim Berglund
  • Enterprise Integration Agility

    by Jeremy Deane
  • Relax with Couch DB

    by Johnny Wey
  • Sass...CSS Evolved

    by Mark Volkmann
Learn More »