Identify the usual Grail environment

Based on this question , I thought I could define something like this (for example)resources.groovy

def currentEnv = Environment.current
if (currentEnv == Environment.CUSTOM && currentEnv.name == 'mock') {
    println 'Do some stuff for the mock env'
}

The code in the if-statement should be executed at startup (for example) grails run-app -Denv=mock, but it is not, what am I doing wrong?

+3
source share
2 answers

You should use a method Environment.executeForCurrentEnvironment(), for example:

import grails.util.Environment

     grails.util.Environment.executeForCurrentEnvironment {
         development {
             println 'Running in DEV mode.'
         }
         production {
             println 'Running in production mode.'
         }
         mock {
             println 'Running in custom "mock" mode.'
         }
     }

and call grails as follows: grails -Dgrails.env=mock run-app

Take a look at this blog post from mrhaki.

+9
source

, , . Grails 2.4.4. mytest myqa, BOTH Grails, Environment.CUSTOM, , bean !

 grails.util.Environment.executeForCurrentEnvironment {
     development {
         println 'Running in DEV mode.'
     }
     mytest {
         println 'This will be evaluated because it is CUSTOM'
     }
     myqa {
         println 'This will ALSO be evaluated because it is CUSTOM. Yikes!'
     }
 }

, . , , :

 switch(Environment.current.name) {
     case 'development':
         println 'Running in DEV mode.'
         break

     case 'mytest':
         println 'Running in mytest mode'
         break

     case 'myqa':
         println 'Running in myqa mode'
         break
 }
+1

All Articles