Grails config files: best practice [closed]
Just wondering what is the 'best practice' when adding config key-value pairs to your grails app.
Should you just add to Config.groovy or create new files.
I tried creating a new config file (Company.groovy) but could not access the config props from my app. However when I cup-p开发者_StackOverflow社区aste the properties into Config.groovy I do have access to them.... This works fine but I dont want Config.groovy to get too large. Also another problem raised its head. While running Integration tests I found that the 'test' env did not have access to my new config properties (values were null).
I must be doing something fundamentally wrong. Any advice would be appreciated.
Thanks, D
I am not sure there are specific best practices. I can tell you plugins that just add a few options usually reuse Config.groovy, but ones that require more complex configuration (i.e. Nimble) typically have their own file that can be separated. So it really depends on your particular needs and amounts of configuration you want to add.
If you examine Config.groovy, you will see in the few top lines (reproduced here) that you can specify where Grails will look for configuration files. It automatically can merge property and .groovy files for you.
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
// grails.config.locations = [ "classpath:${appName}-config.properties",
// "classpath:${appName}-config.groovy",
// "file:${userHome}/.grails/${appName}-config.properties",
// "file:${userHome}/.grails/${appName}-config.groovy"]
This section in the docs describes the options for you.
Just to add the the answer above, if you name your file MySampleConfig.groovy (just end the file name with Config.groovy) its should be added to the grailsApplication context so you should be able to reference the following :
sample{
first{
name = "Italy"
}
second{
name = "Spain"
}
}
as
grailsApplication.config.sample["first"].name
grailsApplication.config.sample["second"].name
Plug ins I believe behave slightly differently and using the ConfigSlurper or Similar is required (I am very open to correction on this one).
def config = new ConfigSlurper().parse(new File('grails-app/conf/MySampleConfig.groovy').toURL())
println config.sample['first'].name
println config.sample['second'].name
精彩评论