4.2 Environments 2.3.8

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 4.2 Environments - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 2.3.8 ## 4.2 Environments #### Per Environment Configuration Grails supports the concept of per environment configuration. The `Config.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"
            }
        }
    }
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`. #### 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]
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 wound run:
    grails test war
To target other environments you can pass a `grails.env` variable to any command:
    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
    }
#### Per Environment Bootstrapping 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/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")
    }
#### Generic 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
        }
    }
| Quick Reference (hide)