37

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!

miek
  • 3,446
  • 2
  • 29
  • 31

4 Answers4

52

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.

John Wagenleitner
  • 10,967
  • 1
  • 40
  • 39
  • This looks like just what I was after! Thank you for your clear and concise answer, John :) – miek Jun 10 '09 at 07:11
  • 11
    This looks like a great solution, except you do have edits after an upgrade. For externalizing the configuration see http://blog.zmok.net/articles/2009/04/22/playing-with-grails-application-configuration – Brad Rhoads Jan 19 '10 at 16:19
  • As Brad pointed out, this isn't a viable solution for deploying a war. The link he posts is a better answer. – Mike Feb 23 '10 at 19:04
  • As neomusashi mentioned, I don't think this is a good solution. Usually you want to have the application configuration outside the application package and source code. – hfm Oct 29 '15 at 16:30
  • one small question, if I don't specify "properties" for the datasource (the info of reconnect,...) will they still be taken from datasource.groovy ? – Dany Y Oct 30 '17 at 11:45
25

The answer above does not really externalize configuration. He is close, but the configuration is still residing in the application. I would use a JVM environment var on startup of the application/server to point to a location outside the application where the external configuration resides. Read out the environment var in the config.groovy file and use it get the external configuration file. Something like this:

def extConfig = System.properties.getProperty('ENVVAR');
grails.config.locations = [ "file:${extConfig}/${appName}-config.groovy"]
Peter De Winter
  • 1,183
  • 2
  • 15
  • 27
1

For me this doesn't work. To get an environment variable.

Better use :

System.getenv().get("ENVVAR").toString()
Sparkup
  • 3,686
  • 2
  • 36
  • 50
Simon
  • 11
  • 1
0

Just put the configuration file location as following in Config.groovy file

grails.config.locations = [
            "file:/yourDirectory/${appName}/${Environment.current.name}-datasource.properties",
            "file:/yourDirectory/${appName}/${Environment.current.name}-config.groovy",
            "classpath:${appName}-${Environment.current.name}-datasource.properties",
            "classpath:${appName}-${Environment.current.name}-config.groovy"
    ]

And put all the details about datasource and other config values in your appropriate file. Hence you can externalize the configuration and need not restart to change values.