Grails external configuration (datasource) with multiple environments

configuration, external, grails

Solution

There are several possible approaches. Here are a couple:

Embed the current environment name in your external config file name:

`grails.config.locations = [ "classpath:app-${grails.util.Environment.current.name}-config.properties"]`

This will cause `app-development-config.properties` to be loaded in dev mode, `app-test-config.properties` in test, etc.

Use the `.groovy` config format instead of `.properties`. With a `.groovy` config file, you can use the `environment { ... }` block.

Problem

In my `Config.groovy` i put line: ``` grails.config.locations = [ "classpath:app-config.properties"] ``` where I set definition for datasource. File looks like: ``` dataSource.url=jdbc:mysql://host/instance dataSource.username=u dataSource.password=p ``` and it properly replace properties from `DataSource.groovy`. Problem is that it replace configuration for every environment, but i need separate config for dev, test and production. Trying to put into file different entries like: ``` environments.development.dataSource.url=jdbc:mysql://host/dev ... environments.production.dataSource.url=jdbc:mysql://host/prod ... ``` ends with default data sources properties defined in `DataSource.groovy`. How to make one property file to work with different environments?

Original source