3.2 环境 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
3.2 环境
Per Environment Configuration
Grails supports the concept of per environment configuration. TheConfig.groovy, DataSource.groovy, and BootStrap.groovy files in the grails-app/conf directory can use per-environment configuration using the syntax provided by ConfigSlurper. As an example consider the following default DataSource definition provided by Grails:dataSource {
pooled = false
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
}
}environments block specifies per environment settings for the dbCreate and url properties of the DataSource.
不同环境(Per Environment)配置
Grails支持不同环境配置的概念。grails-app/conf目录下的Config.groovy、DataSource.groovy和BootStrap.groovy文件都支持 ConfigSlurper 语法的不同环境配置。Grails自带的缺省DataSource定义,就是一个很好的示例:dataSource {
pooled = false
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
}
}environments代码块中DataSource的dbCreate和url属性。Packaging and Running for Different Environments
Grails' command line has built in capabilities to execute any command within the context of a specific environment. The format is:grails [environment] [command name]
dev, prod, and test for development, production and test. For example to create a WAR for the test environment you wound run:grails test war
grails.env variable to any command:grails -Dgrails.env=UAT run-app
不同环境下的运行和打包
Grails的命令行中内置了特定环境下执行命令的能力,其格式为:grails [environment] [command name]
dev、prod和test,分别代表了开发、生产和测试环境。比如,要创建test环境的WAR,可以运行如下命令:grails test war
grails.env变量:grails -Dgrails.env=UAT run-app
Programmatic Environment Detection
Within your code, such as in a Gant script or a bootstrap class you can detect the environment using the Environment class:import grails.util.Environment...switch (Environment.current) { case Environment.DEVELOPMENT: configureForDevelopment() break case Environment.PRODUCTION: configureForProduction() break }
可编程的环境检测
在你的代码中,比如Gant脚本或者启动类,你通过Environment类可以检测到当前的环境,比如:import grails.util.Environment...switch (Environment.current) { case Environment.DEVELOPMENT: configureForDevelopment() break case Environment.PRODUCTION: configureForProduction() break }
Per Environment Bootstrapping
Its often desirable to run code when your application starts up on a per-environment basis. To do so you can use thegrails-app/conf/BootStrap.groovy file's support for per-environment execution:def init = { ServletContext ctx ->
environments {
production {
ctx.setAttribute("env", "prod")
}
development {
ctx.setAttribute("env", "dev")
}
}
ctx.setAttribute("foo", "bar")
}不同环境下的启动
通常你的应用启动时,需要根据不同的环境运行相应的代码,为此你可以使用grails-app/conf/BootStrap.groovy文件来执行不同环境下的处理:def init = { ServletContext ctx ->
environments {
production {
ctx.setAttribute("env", "prod")
}
development {
ctx.setAttribute("env", "dev")
}
}
ctx.setAttribute("foo", "bar")
}Generic Per Environment Execution
The previousBootStrap example uses the grails.util.Environment class internally to execute. You can also use this class yourself to execute your own environment specific logic:Environment.executeForCurrentEnvironment {
production {
// do something in production
}
development {
// do something only in development
}
}不同环境下的通用处理
在以前的BootStrap示例中,我们使用grails.util.Environment类做了内部处理。你也可以通过此类来执行特定环境中的逻辑,比如:Environment.executeForCurrentEnvironment {
production {
// do something in production
}
development {
// do something only in development
}
}
