Targetting different environments with Grails
February 4th, 2009 | Published in java
Mainly as a reminder to myself, here’s how you target different environments with grails.
Grails knows about three environments out of the box: development, test, production. When you create a war file you can specify the environment after the grails command:
grails test war
Sometimes, (actually quite often), you have more environments. For example a user acceptance testing environment. You can easily add configuration information for a new environement in your grails apps. Here are some lines of code from DataSource.groovy, to define the datasource for the acceptance environment:
acceptance {
dataSource {
dbCreate = "update"
jndiName = "jdbc/myAppPool"
}
}
If you want to create a war file for this environment, you need to run grails as follows:
grails -Dgrails.env=acceptance war
Btw, if you want to write code for a specific environment, for example to automatically create some test data in your development environment, you can do this as follows. In BootStrap.groovy you’d write:
class BootStrap {
def init = { servletContext ->
if (GrailsUtil.environment == "development"){
def employee = new Employee(...)
employee.save()
}
}
}
One last remark: i really don’t like targetting different environments when building the software. I think you should be able to ship one and the same war file to 100 customers, without having to change the war file. I also think you should be able to install one and the same war file on all your environments without rebuilding the file. Otherwise your build might introduce bugs that you though you’d fixed.
Leave a Response