Search code examples
grailsgrails-config

Grails app not picking up data from config


I have three environments blocks in my Grails config file similar to this:

environments {
    production {
        grails.serverURL = "https://www.mysite.com"
    }
    development {
        grails.serverURL = "http://localhost:8080/${appName}"
    }
    test {
        grails.serverURL = "http://localhost:8080/${appName}"
    }
}

... // more code

environments {
    production {
        authnet.apiId = "123456"
        authnet.testAccount = "false"
    }
    development {
        authnet.apiId = "654321"
        authnet.testAccount = "true"
    }
    test {
        authnet.apiId = "654321"
        authnet.testAccount = "true"
    }
}

... // more code

environments {
    production {
        email.sales = '[email protected]'
    }
    development {
        email.sales = '[email protected]'
    }
    test {
        email.sales = '[email protected]'
    }
}

Somewhere in a controller:

println grailsApplication.config.grails.serverURL
println grailsApplication.config.authnet.apiId
println grailsApplication.config.email.sales

It prints out:

http://localhost:8080/myapp
[:]
[email protected]

So for some reason the app can't get data from some environments blocks. The stuff outside of environments blocks is fine. I noticed this issue with several different apps, different configs etc. Tried getting it with both grailsApplication and ConfigurationHolder. Is it a Grails bug or I'm doing something wrong? I'm running Grails 1.3.6


Solution

  • Your redefineing your configuration info several times. SInce your writing groovy code that gets executed instead of XML configuration your changes don't get automatically merged, multiple configuration blocks overwrite each other. You need to define everything in one block like

    environments {
        development {
            grails.serverURL = "http://localhost:8080/${appName}"
            authnet.apiId = "654321"
            authnet.testAccount = "true"
        }
        test {
            grails.serverURL = "http://localhost:8080/${appName}"
            authnet.apiId = "654321"
            authnet.testAccount = "true"
        }
        production {
            grails.serverURL = "https://www.mysite.com"
            authnet.apiId = "123456"
            authnet.testAccount = "false"
            }