Get Current Environment in Grails

How to create custom Environments in GRAILS.

This is how you can specify the environments in your grails application. Below we have made five environments.

  • production
  • uat (custom)
  • qa (custom)
  • development
  • test

environments {
    production {
        grails.serverURL = "http://www.ekiras.com/"
    }
    uat {
        grails.serverURL = "http://uat.ekiras.com:8080/"
    }
    qa {
        grails.serverURL = "http://qa.ekiras.com:8080/"
    }
    development {
        grails.serverURL = "http://localhost:8080/${appName}"
    }
    test {
        grails.serverURL = "http://localhost:8080/${appName}"
    }
}

There are Five predefined environments in grails.


  • APPLICATION
  • CUSTOM
  • DEVELOPMENT
  • PRODUCTION
  • TEST

How to get Execute a particular Environment in GRAILS

To run a particular environment in grails you can do the following

grails -Denv=qa -Dserver.port=8080 run-app

How to get Current Environment in Grails Controller

You can get current environment in grails as follows

Environment.current

The above will give either of the above mentioned grails environment. To get the name of the
environment you can do the following

Environment.current.name

and you can do environment specific tasks as follows

switch(Environment.current.name){
            case "development":
                //do your stuff 1
                break;
            case "test":
                //do your stuff 2
                break;
            case "production":
                //do your stuff 3
                break;
            case "qa":  // for custom environment : qa
                //todo:
                break;
            case "uat":  // for custom environment : uat
                //todo:
                break;
        }

How to get Current Environment in Grails View i.e gsp's

<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>
<g:if env="qa">
    We are in Test Mode
</g:if>
<g:if env="uat">
    We are in Test Mode
</g:if>

No comments :

Post a Comment