Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom environment in Grails?

Tags:

grails

groovy

grails.util.Environment, defines a number of preconfigured environments

  • DEVELOPMENT
  • PRODUCTION
  • TEST
  • CUSTOM

When running a Grails command, the environment to use can be specified using a -Denv flag, e.g. grails run-app -Denv=test. You can also specify a block of code that is specific to a certain environment using closures such as:

environments {
    production {
        grails.serverURL = "http://www.changeme.com"
    }
    development {
        grails.serverURL = "http://localhost:8080/${appName}"
    }
    test {
        grails.serverURL = "http://localhost:8080/${appName}"
    }
}

These environment-specific closures can be used in Bootstrap.groovy and Config.groovy, are there other places?

Also, is it possible for me to define my own environment, e.g. PRE_PRODUCTION, such that it will work with the closures above and the -Denv flag?

Finally, can the CUSTOM environment be used with the -Denv flag?

like image 946
Dónal Avatar asked Mar 16 '11 10:03

Dónal


1 Answers

These environment-specific closures can be used in Bootstrap.groovy and Config.groovy, are there other places?

I don't think so... For other places, you would need to use the Generic Per Environment Execution block

Environment.executeForCurrentEnvironment {
    production {
        // do something in production
    }
    development {
        // do something only in development
    }
    pre_production {
        // do something for your custom environment
    }
}

Also, is it possible for me to define my own environment, e.g. PRE_PRODUCTION, such that it will work with the closures above and the -Denv flag?

Yeah, you should be able to just declare -Dgrails.env=pre_production and include the pre_production block in Bootstrap.groovy or Config.groovy (or a custom grails.util.Environment block as above)

edit

As you can see in the Grails source for Environment, this sort of custom environment will enumerate to Environment.CUSTOM, and then in the Environment.executeForCurrentEnvironment block, it will check against CUSTOM, and the name of the custom environment

like image 70
tim_yates Avatar answered Oct 14 '22 08:10

tim_yates