Search

Dark theme | Light theme

December 16, 2009

Grails Goodness: Cleaning Up Before WAR Creation

Grails provides a mechanism where we can execute a closure to do stuff before we create a WAR file. Technically speaking we can change the contents of the staging directory. So when we run the application as an exploded WAR file or we create a WAR file in both cases the closure is executed.

The closure is very useful to delete files we don't want to be in the final WAR file, but are copied by default. We define the closure in conf/BuildConfig.groovy and it must be named grails.war.resources. The closure has a parameter which is the staging directory. The context of the closure is AntBuilder, so all methods we define in the closure are executed for an AntBuilder object. For example if we normally would use the following statement: ant.echo(message: 'Hello'), we must now use echo(message: 'Hello'). The ant object is implicit for the context of the closure.

In the following sample we want to delete the Thumbs.db files Windows generates from the application:

// File: conf/BuildConfig.groovy
grails.war.resources = { stagingDir ->
    echo message: "StagingDir: $stagingDir"
    delete(verbose: true) {
        fileset(dir: stagingDir) {
            include name: '**/Thumbs.db'
        }
    }
}