Externalizing Grails Configurations: The Ultimate Guide

Grails is a wonderful framework built upon Spring, Hibernate, Ant, Junit and more where the coding is only a few minutes ahead, with all the built-in plugins. Grails also uses the dynamic groovy language which makes it more powerful and efficient.

If you are new to grails and have no clue about developing a grails app, please refer the below links for guidance. The below tutorial will give you a quick intro for building your own grails application.

Grails tutorial for beginners

What & Why?

Grails supports environment specific configuration, which is 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 for production (use a remote database).

External configuration files in Grails grants you to extract a fragment from your Config.groovy that are important. Similar to the mail configuration or database configurations which can change by time to time. Sensitive information like your db passwords that we really don’t encourage to be checked into git/subversion/whatever version control you may be using. And these configurations are also packed into deployment package (WAR) during the project build.

If you are like us, looking for ways to decouple the environment specific settings from the grails app, the below steps would save you a lot of time.

The Actual Stuff!

First of all, we need to know about the order in which Grails loads settings from the configuration files.
Grails config and data source are eventually stored in the same configuration object, and during bootstrap, the data source settings are first read from the Config.groovy file (whether directly from a different data source or from an external properties file), and finally read from the datasources.groovy (if it exists).

In order to externalize configurations, you have to create “.groovy” or “.properties” files with the environment specific names like ‘test-Config.groovy” or “production-Config.groovy” and place it in the respective environment server folders outside of your application.

The contents of the external configuration file should be similar to the code below:

grails {
    //DataSource Configuration
    mongo {
        host = '132.12.13.4'
        port = 27017
        username = "Trial"
        password = "*********"
        databaseName = "test"
    }
 
    //Mail Configuration
    mail {
        host = "smtp.mandrillapp.com"
        port = 587
        username = "test@.com"
        password = "**************"
        props = ["mail.smtp.auth":"true",
        "mail.smtp.starttls.enable": "true"]
    }
}

This decouples the environment settings from the application. The Datasource.groovy file inside the application should contain empty configurations for the test environment as below. Easily, these empty properties will be overridden by the external configuration’s dataSource properties.

test {
    grails {
        mongo {
            host =''
            port = 53121
            username = ""
            password = ""
            databaseName = ""
        }
    }
}

Enabling external configuration files in your Grails application becomes easier by just uncommenting the below lines of code spotted at the top of your Config.groovy & replace the paths with the actual path.

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

 

Externalizing Config files without a Restart

It’s always advisable to use the reloadable plugin in grails which provides the facility to reload the external configuration file without a server restart.

Adding the below line in BuildConfig.groovy in the plugins section will do wonders.

runtime "org.grails.plugins:external-config-reload:1.4.1"

https://grails.org/plugin/external-config-reload – link to the plugin doc.

Hurray! That’s it. Whenever a need arises for changing any kind of sensitive data like db password/mail server passwords, redeploying a war is not at all necessary.

Setting permission for External config files

There are scenarios where the configurations are not read due to denied access to the externalized file in the server. To set the permission for the external files placed in the servers, give them the access 600. The owner of the file should be the user that you run tomcat or your other container with.

Problems you may face

If the externalized config.groovy script file has syntax errors, then none of the configurations are read. There won’t any errors logged when the grails app is started.

Note: It’s worth to note that externalized configurations supersede the configurations in any of the Grails configuration files (BuildConfig.groovy, Config.groovy, DataSource.groovy,etc.)

Hope this saves lot of time in creating multiple wars for a simple settings change!