Saturday, February 12, 2011

How do I get at the goodies in my Grails Config.groovy at runtime?

in Config.groovy I see this:

// set per-environment serverURL stem for creating absolute links
environments {
    production {
        grails.serverURL = "http://www.changeme.com"
    }
}

what is the correct way to access that at runtime?

  • here it is:

    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    println ConfigurationHolder.config.grails.serverURL
    

    alternatively, in controllers and tags, apparently this will work:

    grailsApplication.config.grails.serverURL
    

    I needed it in BootStrap, so option 1 was what I needed.

    From danb
  • danb is on the right track. However, life gets a bit easier on your fingers if you do a nicer import:

    import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
    println CH.config.grails.serverURL
    
    danb : very nice.. i was completely unaware of that feature, which I will call import aliasing.

0 comments:

Post a Comment