## 3.2 Environments

#### Per Environment Configuration

Grails supports the concept of per environment configuration. Both the `Config.groovy` file and the `DataSource.groovy` file within the `grails-app/conf` directory can take advantage of 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.hsqldb.jdbcDriver"
        username = "sa"
        password = ""
    }
    environments {
        development {
            dataSource {
                dbCreate = "create-drop" // one of 'create', 'createeate-drop','update'
                url = "jdbc:hsqldb:mem:devDB"
            }
        }
        test {
            dataSource {
                dbCreate = "update"
                url = "jdbc:hsqldb:mem:testDb"
            }
        }
        production {
            dataSource {
                dbCreate = "update"
                url = "jdbc:hsqldb:file:prodDb;shutdown=true"
            }
        }
    }

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`. This syntax can also be used within `Config.groovy`.

#### 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 could do:

    grails test war

If you have other environments that you need to target 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

Its 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
        }
    }