Environment specific externalized Grails configuration

Grails supports environment specific configuration, which can be used to define different settings for development, test, production or custom environments. The Grails User Guide describes this feature in detail. E.g. you can define different database settings for development (use a local test database) and production (use a remote database).

Besides that, Grails supports externalized configuration (again see Grails User Guide). You can define one or more config files (properties or groovy config files supported). The contents of the config files will be merged with the Config.groovy config file in your application (external config file will override existing Config.groovy settings with the same name). This is extremly useful to separate database settings from your sourcecode.

Here is an example how you can define an external config file with the name of your app in the user’s home directory:

// Config.groovy
grails.config.locations = [ "file:${userHome}/${appName}-config.properties" ]

To get the best of both worlds, you can combine these two features: use a general external config file and one external config file for each environment. The environment specific config file will have precedence over the general external config file.

// Config.groovy
grails.config.locations = [ "file:${userHome}/${appName}-config.properties",
"file:${userHome}/${appName}-${grails.util.Environment.current.name}-config.properties" ]

When you start your app in development mode, the following external config files will be picked up (assuming your app has the name myapp):

myapp-config.properties
myapp-development-config.properties

When you start your app in production mode, a different environment specific config file will be picked up:

myapp-config.properties
myapp-production-config.properties
Short URL for this post: http://wp.me/p4nxik-F0
This entry was posted in Groovy and Grails and tagged , , . Bookmark the permalink.

One Response to Environment specific externalized Grails configuration

  1. Tak says:

    If you make you external configuration file a .groofy file, you can also use the environment { } block inside the config file if that’s easier for your situation.

    // Config.groovy
    grails.config.locations = [ “file:${userHome}/${appName}-config.groovy” ]

    //
    appname-config.groovy :
    appname {
    anayticscode = “UA-xxxx-1”
    }
    environments {
    development {
    appname.feature.enabled = true
    }
    production {
    appname.feature.enabled = false
    }
    }

Leave a Reply