Reputation: 187399
Based on this question, I thought I could define something like this in (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 when I run (for example) grails run-app -Denv=mock
but it isn't, what am I doing wrong?
Upvotes: 3
Views: 3187
Reputation: 716
Note that this may be a problem if you have multiple custom environments defined. I am using Grails 2.4.4. If I have a mytest and myqa environment defined, BOTH will be executed by Grails because both are Environment.CUSTOM, meaning some bean definitions are overwritten or misconfigured!
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!'
}
}
I don't know if that's a bug or by design. Anyway, here is what I do and this seems to work properly:
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
}
Upvotes: 1
Reputation: 1865
You must use the method Environment.executeForCurrentEnvironment()
, like this:
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 this way: grails -Dgrails.env=mock run-app
Take a look at this blogpost, from mrhaki.
Upvotes: 9