How do I know the Grails application, in what environment is it located?

I would like to load the environment-specific configuration in my grails application, so depending on which JVM the grails application is running in, I may point to these environment-specific URLs. In my case, I have 4 different environments to work with (instead of the standard 3 that the grails application assumes) when my application goes from dev to prod.

All my JVMs have the entire System property defined, so when I do "System.getProperty ()", tell me in which environment this application works.

My question is, what is the best way to check and load environment configurations at runtime ? Inside BootStrap.groovy? I have no way to build my war file using the command line or grails {env_name} war.

Thanks.

+6
source share
4 answers

Set the grailsEnv variable as an environment. The following is the Java variable for Tomcat:

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

At the grails command line, you add an environment variable:

 grails run-app -Dgrails.env=stage 

You can use the environment variable check as follows:

  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 Enumerated values ​​instead of the name variable, but if you use custom environment values, then they are mapped to a normal enumeration and use the name to distinguish between custom values.

  if (grails.util.Environment.current == grails.util.Environment.DEVELOPMENT) { 
+13
source

Without configuring the JVM startup parameter:

 -Dgrails.env=whatever 

Your grails application will use the value set to

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

A value similar to this will be set:

 grails.env=development 

This default value for the environment is determined by what parameters are used in the construction of the war. You can build a war with

 -Dgrails.env=development war 

Then in application.properties will have grails.env = development, if you leave this, the default value is grails.env = production

As for your question, you do not know what is configured to use "environment-specific URLs". And it's not clear how you store these environment-specific URLs. If, for example, the URL variable is a Grails member variable and you store environment-specific URLs in Config.groovy, then you can

 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 this helps.

+4
source

If you have a System property that tells you which environment you are in, you can simply add if statements or a switch statement to your Config.groovy file, for example:

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

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

Grails configuration files are processed using groovy ConfigSlurper, so you can put executable code there without any problems.

+1
source

Sorry, this is too late, but another way is to enter the configuration property in BootStrap.groovy.

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 very well. We are using Grails 2.5.2

0
source

Source: https://habr.com/ru/post/974429/


All Articles