Bootstrapping, An Environmentalist's Perspective
Grails gives you a handy little class called Bootstrap.groovy. As it’s name implies this class allows you to “bootstrap” your application at startup (and shutdown). This is pretty handy in development when you want your application to start with some sample data loaded. For example, you might use Bootstrap.groovy to create a bunch of test user accounts (that’s what we do for Sierra). As you start to move your application into production you might find yourself commenting out lines of code (I did) in Bootstrap.groovy so that the production version of your application does not launch with all of your sample data loaded.
This week as I was working on some administrative functions for Sierra I found myself thinking there had to be a better way, and it turns out there is. As I’ve discussed before when Grails launches it knows what mode/environment it’s running in. The default environments are “development”, “test”, and “production”. The Grails configuration scripts Config.groovy, DataSource.groovy, etc… give you handy “environments” blocks, but what about classes such as Bootstrap.groovy. What should we do there? The answer is the grails.util.Environment class. This class knows what the current running environment is. If we use this in Bootstrap.groovy we now have a way to only load our sample data if we are in “development” mode. This is how it looks:
if (Environment.getCurrent() == Environment.DEVELOPMENT) { // Insert sample data here }
Tags: grails, SaffronSierra
This entry was posted on Monday, September 28th, 2009 at 11:19 am and is filed under SaffronSierra. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply

