Externalizing Grails Datasource configuration

miek picture miek · Jun 9, 2009 · Viewed 23.9k times · Source

Grails 1.x allows using external configuration files by setting the grails.config.locations directive. Is there a similar approach available for externalizing the database configuration in Datasource.groovy (without setting up JNDI)?

It would prove helpful to be able to configure DB credentials in a simple configuration file outside the application.

Thanks in advance!

Answer

John Wagenleitner picture John Wagenleitner · Jun 10, 2009

You can use a properties file specified in the grails.config.locations as a way to externalize the datasource configuration. Below is how I typically set up a Grails project:

In my DataSource.groovy I specify this for the production environment:

  ....
  ....
  production {
    dataSource {
        dbCreate = "update"
        driverClassName = "com.myorg.jdbcDriverNotExists"
        url = ""
        username = ""
        password = ""
    }
  }
  ....
  ....

I specify an external properties file in my Config.groovy:

grails.config.locations = [ "classpath:app-config.properties"]

In the properties file (stored in grails-app/conf/) I specify the actual datasource info:

dataSource.driverClassName=oracle.jdbc.OracleDriver
dataSource.url=jdbc:oracle:thin:@host:port:sid
dataSource.username=sa
dataSource.password=secret

I also use the properties file as a way to override other values that are in Config.groovy. When the app is deployed, if I have to modify the datasource info I just edit the /WEB-INF/classes/app-config.properties file and restart.