Search code examples
grailsgrails-2.0grails-config

How to tell Grails application which environment it is in?


I would like to load Environment specific configurations in my grails application so that depending on which JVM the grails application is running on, I can point to that environment specific urls. In my case, I have 4 different environments to work with (instead of the default 3 that grails app assumes) when my app goes from dev to prod.

My JVMs all have a System property defined that, when I do "System.getProperty()", tell me which environment that application is running on.

My question is, what is the best place to check and load the environment-specific configurations during run-time? Inside BootStrap.groovy? I do not have the option to build my war file using command line or grails {env_name} war.

Thanks.


Solution

  • Set the variable grailsEnv as a environment Java variable for Tomcat below is an example:

    set CATALINA_OPTS=%CATALINA_OPTS% -Xms256m -Xmx1024m -Dgrails.env=development
    

    On a grails command line you add the environment variable:

    grails run-app -Dgrails.env=stage
    

    You can use check the environment variable like this:

        if (grails.util.Environment.current.name == "development") {
            UsageCodeDefinition ucd = new UsageCodeDefinition()
            ucd.setDescription("UFARSFileUpload Upload Development")
            ucd.setFiscalYear("12-13")
            ucd.setInstructions("Welcome to UFARSFileUpload Development were Open")
            ucd.save(failOnError: true)
        }
    

    You can use the Enumerated values instead of the name variable but if you use custom environmental values then they are mapped to the enumeration custom and using the name works to distinguish between the custom values.

       if (grails.util.Environment.current == grails.util.Environment.DEVELOPMENT) {