Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails external configuration (datasource) with multiple environments

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?

like image 400
Gorky Avatar asked Feb 17 '23 04:02

Gorky


1 Answers

There are several possible approaches. Here are a couple:

  1. 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.

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

like image 65
Andrew Avatar answered May 01 '23 10:05

Andrew