Corporations even in our childrens fantasy? Perl Best Practices
Jun 22

Gravy: Using Ant from Groovy for short non-build scripts

Groovy, Java, Tech Add comments

I recently had to whip out a one/few-off script that took a directory, and recursively grabbed jar files from it, and put all of the classes in one place. I looked at JarJar and Uberjar, but in the end it was easier to whip together a few lines of Groovy to make it happen (although I could have used Ruby, JavaScript/Rhino, Perl, etc etc).

When it came to jar’ing and unjaring files, instead of using the Java APIs, it was easier to just use ant via the AntBuilder. This allowed me in one simple line to use the tasks available there:

ant.unjar(src: file.path, dest: structure[typeOfJar].outputDir)

Very convenient. This isn’t using Ant in a more formal “I am driving the build from Groovy” way, but rather being pragmatic and thinking “oh there is a simple ant task that already does this, so lets just call into it”.

Example

def buildJar(String typeOfJar) {
println "Building a jar file for the type: $typeOfJar"
new java.io.File(structure[typeOfJar].dir).eachFileRecurse { file ->
try {
//println file.path
if (file.path =~ "\\.jar") {
println "jar xf $file.path $structure[typeOfJar].outputDir";
ant.unjar(src: file.path, dest: structure[typeOfJar].outputDir)
}
} catch (Exception e) {
println e
}
}

ant.jar(destfile: structure[typeOfJar].jar, basedir: structure[typeOfJar].outputDir)
ant.delete(dir: structure[typeOfJar].outputDir)
}

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'cricket'