The above can be expressed in Groovy syntax in application.groovy
as follows:
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"
}
}
}
Notice how the common configuration is provided at the top level and then an environments
block specifies per environment settings for the dbCreate
and url
properties of the DataSource
.
In addition, there are 3 preset environments known to Grails: dev
, prod
, and test
for development
, production
and test
. For example to create a WAR for the test
environment you would run:
grails test war
To target other environments you can pass a grails.env
variable to any command:
...
switch (Environment.current) {
case Environment.DEVELOPMENT:
configureForDevelopment()
break
case Environment.PRODUCTION:
configureForProduction()
break
}
It’s often desirable to run code when your application starts up on a per-environment basis. To do so you can use the grails-app/init/BootStrap.groovy
file’s support for per-environment execution:
The previous BootStrap
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