Search code examples
grailsenvironment-variablesgsp

How to inject environments variable into Grails GSP file?


Say I have the following Grails (2.3.6) GSP file:

<%
    String env = ??? how to get from the -Dgrails.env arg below?
%>
<head>
    <!-- head stuff -->
</head>
<body>
    <h2>I am running in the ${env} environment!</h2>
</body>

Now let's say I run my Grails app with different environments specified:

grails -Dgrails.env=dev run-app

grails -Dgrails.env=fizz run-app

grails -Dgrails.env=buzz run-app

...etc. How can I inject the -Dgrails.env argument into the env variable in the GSP file?


Solution

  • Try this:

    <%
        String env = grails.util.Environment.current.name
    %>
    
    My Environment = ${env}