Grails external configuration (data source) with multiple environments

In my Config.groovy line Config.groovy I put the line:

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

where I set the definition for the data source. The file looks like this:

 dataSource.url=jdbc:mysql://host/instance dataSource.username=u dataSource.password=p 

and it will correctly replace the properties from DataSource.groovy .

The problem is that it replaces the configuration for each environment, but I need a separate configuration for dev, test and production. Trying to write different records to a file, for example:

 environments.development.dataSource.url=jdbc:mysql://host/dev ... environments.production.dataSource.url=jdbc:mysql://host/prod ... 

ends with the default data source properties defined in DataSource.groovy . How to make one properties file for working with different environments?

+4
source share
1 answer

There are several possible approaches. Here is a couple:

  • Paste the name of the current environment into the name of the external configuration file:

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

    This will load app-development-config.properties in dev mode, app-test-config.properties in the test, etc.

  • Use the .groovy configuration format instead of the .properties . With the .groovy configuration file .groovy you can use the environment { ... } block environment { ... } .

+8
source

Source: https://habr.com/ru/post/1469023/


All Articles