Externalize datasource configuration in Grails

the following seems to work to externalize datasource configuartion (see http://jira.codehaus.org/browse/GRAILS-3226?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel)
Basically
1) you add the grails.config.locations property in the Config.groovy like below

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

Notice: I have added the file is users file system , so the user can change it, without touching the war file . Restarting the app should pick up the new value
2) in the ${appName}-config.properties” file

dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost/something
dataSource.username=root
dataSource.password=secret

Notice no quotes in the value
I am using grails 1.2.1

Tags: , ,

2 Responses to “Externalize datasource configuration in Grails”

  1. toddgator Says:

    Have you successfully used the ‘classpath:{filename.properties}’ method for defining the configuration file? I like the modular style of having environment specific configuration files outside of Config.groovy, but don’t want to have to worry about files outside of the WAR once deployed. I’ve tried it and put my *.properties files in ‘grails-app/conf’, but it always returns an error regarding not being able to find the file specified in Config.groovy. If it helps, this is what I included in Config.groovy:

    environments {
    development {
    grails.config.locations << "classpath:dev-config.properties"
    }
    prod {
    grails.config.locations << "classpath:prod-config.properties"
    }
    etc….
    }

    I've also tried specifying "classpath:grails-app/conf/{env}-config.properties" with no luck either. I guess my queston is once you use the classpath option is there a specific location in the grails project that these config files should be put?

  2. redtoledo Says:

    Looks like a chicken and egg problem
    see
    http://grails.1312388.n4.nabble.com/using-an-external-property-w-in-Config-groovy-tp3095970p3096041.html

    You probably thought of the following and trying to avoid it — but just in case
    http://stackoverflow.com/questions/11469276/can-i-split-config-groovy-file-of-grails

    “It might not be in the classpath yet, there’s a bug where the properties file won’t be accessible after a clean but will work on the next run-app. But if that’s happening you’d see a warning on the console saying it couldn’t load the file.

    But you’re also affected by timing. You specify the external files in Config.groovy but they’re resolved after Config.groovy is parsed, so you can’t use the values inside the file. If you run ‘grails console’ and execute ‘println grailsApplication.config.test.name’ it should print the correct value.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: