Tuesday, August 19, 2008

Grails' JAR build time check

A while back, there was a discussion on the grails-user mail list regarding using a different version of commons-lang than the one provided by Grails. The common answer on the thread was, "just try it out...".

Ok, great. With that advice I went into $GRAILS_HOME/lib and I replaced commons-lang-2.1.jar with commons-lang-2.4.jar. My reason for doing so was that I wanted to use some methods on the DateUtils class that were introduced in commons-lang 2.2. My only question was this: 'how could I ensure that I would always have at least commons-lang 2.2 in my project when deploying as a war?'

Having very good test coverage would be one answer :) or another would be checking that when a WAR file is built that the commons-lang version is checked at build time. With my implementation, I ensure that I only have one version of commons-lang in my project and that the JAR's manifest file details the version expected. Here's the script.

$PROJECT_HOME/scripts/Events.groovy:
import java.util.jar.JarFile

eventWarStart = {
String nm = "commons-lang"
def min = 2.2
def jars = new File("$stagingDir/WEB-INF/lib").listFiles() as List
def commLang = jars.findAll { return it.name.startsWith(nm) }
if (!commLang || commLang.size() != 1) throw new RuntimeException("[fail] Commons lang problem")
def manifest = new JarFile(commLang[0]).manifest
if (!manifest) throw new RuntimeException("No manifest in ${commLang[0].file}")
def vers = manifest?.mainAttributes?.getValue("Implementation-Version")?.toBigDecimal()
if (!vers || vers < min) throw new RuntimeException("[fail] Illegal $nm version: ${vers}. Minimum $min")
}

0 comments: