Grails - Environments in BuildConfig
As of Grails 2.3 it is possible to use the
Environment class everywhere in
BuildConfig.groovy. This can be useful for environment-specific settings regarding all the configuration that can be done in
BuildConfig.groovy including dependencies and plugins.
What’s the deal?
Recently I came across an issue with the UI performance plugin (like mentioned by someone on the Grails user list) where we needed to exclude dependencies to be only used in the
test environment. Before Grails 2.3.x it was only possible to use the
Environment class inside the
This little trick was possible because the closure was evaluated at a point of time where the
Environment already had been initialized. Something along the lines of
wouldn’t have been successful caused by the
Environment class not being initialized at runtime.
This behaviour has been fixed in Grails 2.3, the BuildConfig DSL has even been extended to support the environments block feature. We can use it to specify environment-specific dependencies blocks or other BuildConfig configuration settings:
Before 2.3.x the
Environment could only be used restrictively in
BuildConfig.groovy. With 2.3.x this issue has been fixed and support for the
environments block has been added to the BuildConfig DSL.