The above can be expressed in Groovy syntax in application.groovy as follows:

    1. dataSource {
    2. pooled = false
    3. driverClassName = "org.h2.Driver"
    4. username = "sa"
    5. password = ""
    6. }
    7. environments {
    8. development {
    9. dataSource {
    10. dbCreate = "create-drop"
    11. url = "jdbc:h2:mem:devDb"
    12. }
    13. test {
    14. dataSource {
    15. dbCreate = "update"
    16. url = "jdbc:h2:mem:testDb"
    17. }
    18. production {
    19. dataSource {
    20. dbCreate = "update"
    21. url = "jdbc:h2:prodDb"
    22. }
    23. }
    24. }

    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:

    1. grails test war

    To target other environments you can pass a grails.env variable to any command:

    1. ...
    2. switch (Environment.current) {
    3. case Environment.DEVELOPMENT:
    4. configureForDevelopment()
    5. break
    6. case Environment.PRODUCTION:
    7. configureForProduction()
    8. break
    9. }

    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:

    1. Environment.executeForCurrentEnvironment {
    2. production {
    3. // do something in production
    4. }
    5. development {
    6. // do something only in development