How to tell Grails application which environment it is in?

15.3k Views Asked by At

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.

5

There are 5 best solutions below

2
On BEST ANSWER

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) {
0
On

As an addendum to the other answers:

You can use Environment.isDevelopmentMode() or in a groovier way Environment.developmentMode to check if the environment is set to development. This is useful when you take the aproach of only modifying settings for development on your code where production settings are default.

0
On

Without setting the JVM startup parameter:

-Dgrails.env=whatever

Your grails app will use the value set in

<yourapp>/WEB-INF/classes/application.properties

There will be a value set like this:

grails.env=development

This default environment value is determined by what options are used when building the war. You can build the war with

-Dgrails.env=development war

Then the application.properties will have grails.env=development, if you leave that off, it defaults to grails.env=production

As far as your question, you are not specific about what is being configured to use "environment specific urls". And it is not clear how you are storing these environment specific urls. If, for example, the URL variable is a member variable of a Grails service and you are storing the environment specific URLs in the Config.groovy, then you could

import grails.util.Environment

...
//inject the GrailsApplication Configuration in Config.groovy
def grailsApplication
//Hold the URL value from Config.groovy
String environmentUrl
...
Environment current = Environment.getCurrent()

if(Environment.PRODUCTION == current) { 
    environmentUrl = grailsApplication.config.PRODUCTION_URL
} else {
    environmentUrl = grailsApplication.config.DEVELOPMENT_URL
}

Where Config.groovy has

PRODUCTION_URL = "http://blah.com/blah/"
DEVELOPMENT_URL = "http://blah.dev/blah"

Hope that helps.

0
On

If you have a System property available that tells you what environment you're in you can simply add if statements or a switch statement in your Config.groovy file, like this:

if (System.getProperty("foo") == "myTestEnvironment") {
    myConfigSetting = "test"
} else if (System.getProperty("foo") == "myProductionEnvironment") {
    myConfigSetting = "production"
}

This solution also works in other config files under grails-app/conf

Grails config files are parsed using groovy ConfigSlurper so you can put executable code in there without a problem.

0
On

Sorry this is way late, but another way is to inject a configuration property in BootStrap.groovy.

For Example:

if (currentEnv == Environment.DEVELOPMENT) {
    ...
    grailsApplication.config.some.property = DEVELOPMENT_ENVRIONMENT
    ...
}
else if (currentEnv == Environment.TEST) {
    ...
    grailsApplication.config.some.property = TEST_ENVIRONMENT
    ...
}

I have used this recently and it works really well. We are using Grails 2.5.2