Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 420
Stealth Avatar asked Aug 27 '14 17:08

Stealth


4 Answers

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.

like image 112
fourgablesguy Avatar answered Nov 14 '22 12:11

fourgablesguy


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) {
like image 27
Joe Avatar answered Nov 14 '22 11:11

Joe


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.

like image 26
MattZ Avatar answered Nov 14 '22 11:11

MattZ


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.

like image 34
Maicon Mauricio Avatar answered Nov 14 '22 11:11

Maicon Mauricio