微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐