Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Enable external configuration in Grails 3

External configuration in Grails 3

Working on Grails 3 I realized that I no longer can specify external configuration using the standard grails.config.locations property in Config.groovy file.

Reason is obvious! There is no Config.groovy now in Grails 3. Instead we now use application.yml to configure the properties. However, you can't specify the external configuration using this file too!

What the hack?

Now Grails 3 uses Spring's property source concept. To enable external config file to work we need to do something extra now.

Suppose I want to override some properties in application.yml file with my external configuration file.

E.g., from this:

    username: sa
    driverClassName: "org.h2.Driver"

To this:

    username: root
    password: mysql
    driverClassName: "com.mysql.jdbc.Driver"

First I need to place this file in application's root. E.g., I've following structure of my Grails 3 application with external configuration file app-config.yml in place:

[human@machine tmp]$ tree -L 1 myapp
├── app-config.yml // <---- external configuration file!
├── build.gradle
├── gradle
├── gradlew
├── gradlew.bat
├── grails-app
└── src
[human@machine tmp]$ 

Now, to enable reading this file just add following:

  • To your build.gradle file
bootRun {
    // local.config.location is just a random name. You can use yours.
    jvmArgs = ['-Dlocal.config.location=app-config.yml']
  • To your Application.groovy file
package com.mycompany

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.PropertiesPropertySource

class Application extends GrailsAutoConfiguration implements EnvironmentAware {
    static void main(String[] args) {, args)

    void setEnvironment(Environment environment) {
        String configPath =["local.config.location"]
        Resource resourceConfig = new FileSystemResource(configPath)
        YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean()
        ypfb.setResources([resourceConfig] as Resource[])
        Properties properties = ypfb.getObject()
        environment.propertySources.addFirst(new PropertiesPropertySource("local.config.location", properties))

Notice that Application class implements EnvironmentAware Interface and overrides its setEnvironment(Environment environment):void method.

Now this is all what you need to re-enable external config file in Grails 3 application.

Code and guidance is taken from following links with little modification:




This comment has been minimized.

Copy link

@gabehamilton gabehamilton commented Feb 23, 2016

Great gist, thanks. In my fork I added a check for whether the file exists in case the local config is optional.


This comment has been minimized.

Copy link

@javagrails javagrails commented May 22, 2016

after [ grails prod war ] -> i didnt find any app-config.yml file in project root folder


This comment has been minimized.

Copy link

@robertoschwald robertoschwald commented Oct 11, 2016

Isn't the spring-boot mechanism using SPRING_CONFIG_LOCATION env var sufficient?


This comment has been minimized.

Copy link

@magnomp magnomp commented Dec 1, 2016

It's important to note that this mechanism and the default grails/springboot mechanism behave diferently when dealing with lists
While this:
- a
- b

by default is loaded as == ['a', 'b']`,
when loaded by this mechanism it's loaded as[1] == 'a'[2] == 'b'

It took me a lot of time to figure out this inconsistence


This comment has been minimized.

Copy link

@magnomp magnomp commented Dec 1, 2016

Just found out that you can use YamlMapFactoryBean instead of YamlPropertiesFactoryBean to get the standard behaviour


This comment has been minimized.

Copy link

@tyagiakhilesh tyagiakhilesh commented May 5, 2017

Better way can be using runtime.groovy file (to be placed alongside application.groovy and then I can do something below and it works alright.

dataSource {

    url = "jdbc:h2:mem:devDb" // DO NOT do 'def url = "...."', this shall be treated as a variable, not config option.
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
    pooled = true

    ConnectionService connectionService = new ConnectionService(false)
    if (connectionService.init()) {
        Map connectionMap = connectionService.getConnectionMap()

        url = (String)connectionMap.get('url')
        driverClassName = (String)connectionMap.get('driverClassName')
        username = (String)connectionMap.get('username')
        password = (String)connectionMap.get('password')

        RDBMSType rdbmsType = connectionMap.get('rdbmsType');
        if (rdbmsType.getRDBMSName().contains("oracle")) {
            dialect = "org.hibernate.dialect.Oracle10gDialect"
        OBTLog.debug('Connecting to url: ' + url)
        OBTLog.debug('  driverClassName: ' + driverClassName)
        OBTLog.debug('         username: ' + username)
    } else {
        // Use default h2 in mem database; don't override.

println "DataSource being configured is $dataSource"

hibernate {
    cache {
        queries = false
        use_second_level_cache = true
        use_query_cache = false
        region {
            factory_class = org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

    hbm2ddl {
        ConnectionService connectionService = new ConnectionService(false)
        if (connectionService.init()) {
            auto = "update"
        } else {
            auto = "create"

This comment has been minimized.

Copy link

@rstuven rstuven commented Aug 22, 2017

"This plugin will mimic the Grails 2 way of handling external configurations defined in grails.config.locations."


This comment has been minimized.

Copy link

@paulphilimone paulphilimone commented Dec 9, 2017

@javagrails as a point.

After "grails war" that "app-config.yml" file wont be inside the war or the deployed directory, and the configuration will not be loaded.

There is a workaround to that: on "src/main" directory create other dir named "resources" (src/main/resources) and put the configuration file (app-config.yml) there.

On Application.groovy class Load the file with this:

def url = getClass().classLoader.getResource("app-config.yml")
def confFile = new File(url.toURI())
Resource resourceConfig = new FileSystemResource(confFile)

Like that u will be able to load the external configuration file in production and development mode.


This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment