user689842
user689842

Reputation:

Grails access properties from environment file

In my Config.groovy I consolidate properties files in the following way:

grails.config.locations << "file:${userHome}/environment.properties" << "file:${userHome}/passwords.properties"

I have two problems:

  1. When autowiring in the Service layer, with GrailsApplication I can retrieve properties defined in Config.groovy BUT NOT those properties defined in the files
  2. I cannot autowire GrailsApplication in my Jobs (using Quartz plugin)

Can anyone shed light on these issues?

Upvotes: 0

Views: 2072

Answers (2)

felipenasc
felipenasc

Reputation: 448

What I do is to have an external Config.groovy file, for instance: MyConfig.groovy

At the end of the standard grails Config.groovy file, I have the following:

def ENV_NAME = "MY_EXTERNAL_CONFIG"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []
}
if(System.getenv(ENV_NAME)) {
    grails.config.locations << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
    grails.config.locations << "file:" + System.getProperty(ENV_NAME)
} else {
    println "No external Configs found."
}

So now you can have a MyConfig.groovy file anywhere in production environment (for example) and then set an Environment system variable to point to this file (or pass it as parameter to startup.sh), before you start tomcat:

MY_EXTERNAL_CONFIG="/home/tomcat/configs/MyConfig.groovy"
export MY_EXTERNAL_CONFIG

That's it. Now you have an external MyConfig.groovy file. The properties in it are accessible from your grails app as they were part of the standard Config.groovy

import org.codehaus.groovy.grails.commons.*
//... 
ConfigurationHolder.config.foo.bar.hello

Upvotes: 1

Arthur Neves
Arthur Neves

Reputation: 12128

how about trying this!(just a guess, it should not make any difference! however give it a shot and let me know if doesnt work!)

grails.config.locations = [ "file:${userHome}/environment.properties",
                            "file:${userHome}/passwords.properties" ]

Upvotes: 0

Related Questions