Grails Cookbook - A collection of tutorials and examples

How To Check If Environment is Test, Development, or Production in Grails

Sometimes it is very helpful to execute some code depending on which environment you are in. For example, you may want to insert test data inside Bootstrap when you run the application in development mode, but not in production mode.
This is simple in Grails on different sections of your application.

Bootstrap

As discussed above, it is common practice to insert different set of data in Bootstrap depending on the environment you are currently running. This is the sample code.
import grails.util.Environment
class BootStrap {
    def init = { servletContext ->
        if (Environment.current == Environment.DEVELOPMENT) {
            // insert Development environment specific code here
        } else 
        if (Environment.current == Environment.TEST) {
            // insert Test environment specific code here
        } else 
        if (Environment.current == Environment.PRODUCTION) {
            // insert Production environment specific code here
        } 
    }
}

Controller

The same code can be used in Controllers
package asia.grails.myexample
import grails.util.Environment
class SomeController {
    def someAction() { 
        if (Environment.current == Environment.DEVELOPMENT) {
            // insert Development environment specific code here
        } else 
        if (Environment.current == Environment.TEST) {
            // insert Test environment specific code here
        } else 
        if (Environment.current == Environment.PRODUCTION) {
            // insert Production environment specific code here
        }
        render "Environment is ${Environment.current}"
    }
}

View/GSP

In view, we can use the if tag:
<g:if env="development">
    We are in Development Mode
</g:if>
<g:if env="production">
    We are in Production Mode
</g:if>
<g:if env="test">
    We are in Test Mode
</g:if>
Tags: environment