Config
Since 0.6, general Grails and application configuration that is not possible via convention is handled by the
Config.groovy file in
grails-app/conf .
This file uses Groovy's
ConfigSlurper that provides an enhanced properties file like format.
The
Config.groovy file can be used to configure Log4j
LoggingBuilt in Grails config options
Grails also provides the following configuration options:
- grails.enable.native2ascii - Set this to true if you require native2ascii conversion of Grails i18n properties files
grails.enable.native2ascii=true
Per environment Configuration
You can have per-environment configuration as shown below. A configuration property set outside of
environments may be overridden. In the example shown below,
setting will have a value of
foo except when run in the production environment, in which case
setting will have a value of _bar_.
setting = "foo"
com.example.anything.setting = "something"
environments {
production {
setting = "bar"
}
}Available/Bound Variables in Config.groovy
| Name | Value | Example-value |
|---|
| userHome | System.getProperty("user.home") | /home/martin |
| grailsHome | System.getProperty("grails.home") | /opt/grails |
| appName | application.properties: app.name | book |
| appVersion | application.properties: app.version | 0.1 |
The binding happens in
DefaultGrailsApplication.getConfig() .
Accessing the configuration from your code
There are two options for getting hold of the config object:
-
grailsApplication.config , where 'grailsApplication' is an instance of GrailsApplication;
-
ConfigurationHolder.config .
Here are some examples:
// Accessing configuration data from a controller…
class MyController {
…
def list = {
def propValue = grailsApplication.config.setting
def otherPropValue = grailsApplication.config.com.example.anything.setting
…
}
…
}// ...and from elsewhere
import org.codehaus.groovy.grails.commons.ConfigurationHolderclass Something {
def myData = ConfigurationHolder.config.setting
def myOtherData = ConfigurationHolder.config.com.example.anything.setting
…
}Hint for editing the config file
Be aware that if you insert configuration properties using the builder notation, you may overwrite preceding properties. For example:
grails.views.default.codec="none"
grails.views.gsp.encoding="UTF-8"
grails.converters.encoding="UTF-8"
grails.enable.native2ascii = true
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youracount@gmail.com"
password = "yourpassword"
}
}In this case the mail configuration would overwrite all the
grails.xx properties set above. In such a case write the mail configuration before defining other
grails.xx properties or write it as grails.mail.host = ...
Build Configuration
Since Grails 1.1 the installed plug-ins are placed in your home directory under a .grails/[PROJECT NAME] directory. This can be changed by creating/editing a configuration file called "BuildConfig.groovy" under the grails-app/conf directory with the following line in it.
grails.project.plugins.dir="./plugins"