How to execute code depending on environment you are in?

You can execute code depending on environment you are in following ways:

In Bootstrap:

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
       }
    }
}   

In Controller:

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}”
         }
}

In Views:

<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>


ProsperaSoft offers Grails development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Grails experts and consultants.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>