How to tell Grails application which environment it is in?
Asked Answered
S

5

12

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.

Several answered 27/8, 2014 at 17:45 Comment(7)
All you need is here: grails.org/doc/latest/guide/conf.html#environments :)Bukovina
Thanks. I read that. I am having trouble understanding how grails knows/tells-apart which environment it is currently in? When the app moves from dev to ,say, prod, how does it know it is prod?Several
It depends. If you run grails app with run-app then you can pass environment name along with run-app command. If you deploy it for example in Tomcat you create war which is environment specific and you can choose environment while building war (grails <env_name> war). It's all described quite nicely in linked documentation page in section 'Packaging and Running for Different Environments'.Bukovina
Thanks Pawel. I really appreciate your help. The setting I work in, I move code from dev...prod using maven and some build scripts. When packaging up the war, we never tell the application the <env_name> since we try to program our app environment-agnostically. That is why, I need to check the environment on the fly when the grails-app comes up and load up the configuarations.Several
If you don't want to build a separate WAR file for each JVM you can add code to check the value of the System property and set the configuration based on that.Radiocommunication
@MattZ, and the best place to do that within grails app is?..Several
You can add your own settings to Config.groovy. BootStrap.groovy is typically used for loading initial data into the database.Radiocommunication
M
23

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) {
Myna answered 27/8, 2014 at 18:16 Comment(2)
This I get. But I am having trouble finding an answer to "what is the best place to check and load the environment-specific configurations during run-time?"Several
Grails uses the values set in an environment DSL like in DataSource.groovy based on what environment you set. Otherwise you can do get the name from grails.util.Environment.current.name like the example above.Myna
K
10

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.

Keelin answered 27/8, 2014 at 21:27 Comment(0)
R
2

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.

Radiocommunication answered 27/8, 2014 at 18:32 Comment(0)
C
1

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

Caryloncaryn answered 16/6, 2016 at 15:33 Comment(0)
H
1

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.

Hothead answered 19/4, 2021 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.