Grails Environment Checking: Better know a framework


Checking the environment that your Grails application is running.

This is nothing new to Grails 1.1, but I use it quite often and is handy to know.

Lets say you have some code that is dependent on the environment that it is running.

Here is one way to check it:

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil

//check if production 
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_PRODUCTION)) ){...}

//check if development
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_DEVELOPMENT)) ){...}

//check if testing
if ( GrailsUtil.getEnvironment().equals(GrailsApplication.ENV_TEST) ){...}

And here is how you can set the environment in your tests:

import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil

//set envionment to production
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_PRODUCTION)

//set envionment to development
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_DEVELOPMENT)

//set envionment to test
System.setProperty(GrailsApplication.ENVIRONMENT, GrailsApplication.ENV_TEST)

  1. No comments yet.
(will not be published)
  1. No trackbacks yet.