如何在Grails中创建自定义环境?

grails.util.Environment定义了一些预配置的环境

>发展
>生产
>测试
>自定义

运行Grails命令时,可以使用-Denv标志来指定要使用的环境. grails run-app -Denv = test.您还可以使用闭包指定特定于特定环境的代码块,例如:

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

这些环境特定的关闭可以在Bootstrap.groovy和Config.groovy中使用,还有其他地方吗?

另外,我有可能定义自己的环境,例如PRE_PRODUCTION,以便它可以使用上面的关闭和-Denv标志?

最后,可以将CUSTOM环境与-Denv标志一起使用吗?

解决方法

These environment-specific closures
can be used in Bootstrap.groovy and
Config.groovy,are there other places?

我不这么认为,对于其他地方,你需要使用Generic Per Environment Execution

Environment.executeForCurrentEnvironment {
    production {
        // do something in production
    }
    development {
        // do something only in development
    }
    pre_production {
        // do something for your custom environment
    }
}

Also,is it possible for me to define
my own environment,e.g.
PRE_PRODUCTION,such that it will work
with the closures above and the -Denv
flag?

是的,您应该能够声明-Dgrails.env = pre_production,并在Bootstrap.groovy或Config.groovy(或上述自定义grails.util.Environment块)中包含pre_production块.

编辑

如您在Grails source for Environment中可以看到的那样,这种定制环境将枚举到Environment.CUSTOM,然后在Environment.executeForCurrentEnvironment块中,它将为check against CUSTOM,and the name of the custom environment

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...