January 24, 2010

Grails Goodness: Simple Script to Create WAR Files for Each Environment

We can create a new WAR file with the following Grails command:

$ grails test war dist/test.war

This will make a new WAR file in the dist directory with the name test.war. We use the test environments for the settings.

With the following Groovy script we create a WAR file in the dist for each environment. We use application.properties to get the application name and version and use it to create the WAR filename.

// File: createwar.groovy
// Run with: $ groovy createwar.groovy
def ant = new AntBuilder()

// Read properties.
ant.property file: 'application.properties'

def appVersion = ant.project.properties.'app.version'
def appName = ant.project.properties.'app.name'

def envs = ['dev', 'test', 'prod', 'testserver1', 'testserver2']
envs.each { env ->
    def grailsEnv = env
    ant.exec(executable: 'grails') {
        arg(value: "-Dgrails.env=${grailsEnv}")
        arg(value: 'war')
        arg(value: "dist/${appName}-${appVersion}-${grailsEnv}.war")