如何在 Grails 中创建自定义环境? [英] How to create a custom environment in Grails?

查看:19
本文介绍了如何在 Grails 中创建自定义环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grails.util.Environment,定义了许多预配置环境

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

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

环境{生产 {grails.serverURL = "http://www.changeme.com"}发展 {grails.serverURL = "http://localhost:8080/${appName}"}测试 {grails.serverURL = "http://localhost:8080/${appName}"}}

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

另外,我是否可以定义自己的环境,例如PRE_PRODUCTION,这样它就能与上面的闭包和 -Denv 标志一起工作?

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

解决方案

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

我不这么认为...对于其他地方,您需要使用 通用每个环境执行

Environment.executeForCurrentEnvironment {生产 {//在生产中做一些事情}发展 {//只在开发中做一些事情}预生产{//为你的自定义环境做一些事情}}

<块引用>

另外,我是否可以定义我自己的环境,例如PRE_PRODUCTION,这样它就可以工作了使用上面的闭包和 -Denv标志?

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

编辑

正如您在 Grails 中看到的那样 source for Environment,这种自定义环境会枚举到Environment.CUSTOM,然后在Environment.executeForCurrentEnvironment块中,它会检查CUSTOM,以及自定义环境的名称

grails.util.Environment, defines a number of preconfigured environments

  • DEVELOPMENT
  • PRODUCTION
  • TEST
  • CUSTOM

When running a Grails command, the environment to use can be specified using a -Denv flag, e.g. grails run-app -Denv=test. You can also specify a block of code that is specific to a certain environment using closures such as:

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

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

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?

Finally, can the CUSTOM environment be used with the -Denv flag?

解决方案

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

I don't think so... For other places, you would need to use the Generic Per Environment Execution block

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?

Yeah, you should be able to just declare -Dgrails.env=pre_production and include the pre_production block in Bootstrap.groovy or Config.groovy (or a custom grails.util.Environment block as above)

edit

As you can see in the Grails source for Environment, this sort of custom environment will enumerate to Environment.CUSTOM, and then in the Environment.executeForCurrentEnvironment block, it will check against CUSTOM, and the name of the custom environment

这篇关于如何在 Grails 中创建自定义环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆