Some deployments require that configuration be sourced from more than one place and be changeable without requiring a rebuild of the application. In order to support deployment scenarios such as these the configuration can be externalized. To do so you need to point Grails at the locations of the configuration files Grails should be using by adding a
grails.config.locations
setting in
Config.groovy
:
grails.config.locations = [ "classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy"]
In the above example we're loading configuration files (both Java properties files and
ConfigSlurper configurations) from different places on the classpath and files located in
USER_HOME
.
It is also possible to load config by specifying a class that is a config script.
grails.config.locations = [com.my.app.MyConfig]
This can be useful in situations where the config is either coming from a plugin or some other part of your application. A typical use for this is re-using configuration provided by plugins across multiple applications.
Ultimately all configuration files get merged into the
config
property of the
GrailsApplication object and are hence obtainable from there.
Values that have the same name as previously defined values will overwrite the existing values, and the pointed to configuration sources are loaded in the order in which they are defined.
Config Defaults
The configuration values contained in the locations described by the
grails.config.locations
property will
override any values defined in your application
Config.groovy
file which may not be what you want. You may want to have a set of
default values be be loaded that can be overridden in either your application's
Config.groovy
file or in a named config location. For this you can use the
grails.config.defaults.locations
property.
This property supports the same values as the
grails.config.locations
property (i.e. paths to config scripts, property files or classes), but the config described by
grails.config.defaults.locations
will be loaded
before all other values and can therefore be overridden. Some plugins use this mechanism to supply one or more sets of default configuration that you can choose to include in your application config.
Grails also supports the concept of property place holders and property override configurers as defined in Spring For more information on these see the section on Grails and Spring