Skip to content

Groovy Build Script Using AntBuilder Example

As with most things in the life of a Java developer, doing simple things are simple, more complex things though are often much more complicated. For self-described slow learners such as myself, complex tasks become exponentially slower. So it has been with my adoption of the Groovy programming language. Even with the book, it’s still hard to find examples or adequate documentation for performing tasks the groovy way.

I have written a set of scripts to perform a data migration, but stalled when I went to write a groovy build script. Here’s what I ended up with:

import org.apache.log4j.Logger
import org.apache.tools.ant.listener.Log4jListener
import org.apache.tools.ant.Project

class build {

def ant = new AntBuilder()
static def SRC_DIR 		 = "Source/groovy"
static def DIST_DIR 	         = "dist"
static def CLASSES_DIR        = "bin"
static def LIB_DIR		  = "Source/lib"

def classpath = ant.path {
fileset(dir: "${LIB_DIR}"){
include(name: "*.jar")
}
pathelement(path: "${CLASSES_DIR}")
}

def groovyclasspath = ant.path {
fileset( file: "${LIB_DIR}/groovy-all-1.0.jar")
}

static void main(args) {
def build = new build()
build.run(args)
}

def all() {
ant.mkdir(dir: "${CLASSES_DIR}")
ant.javac(
destdir: "${CLASSES_DIR}",
srcdir: "${SRC_DIR}",
classpath: "${classpath}")
ant.groovyc(
destdir: "${CLASSES_DIR}",
srcdir: "${SRC_DIR}",
includes: "*.groovy",
classpathref: "${LIB_DIR}")
}

def void run(args) {
ant.getProject().addBuildListener(new Log4jListener())
ant.taskdef(
name: "groovyc",
classname: "org.codehaus.groovy.ant.Groovyc")
if ( args.size() > 0 ) {
invokeMethod(args[0], null )
} else {
all()
}

}

The problem with this is that the basic ant logging is gone. This is all I see when I run it with no arguments:

14:19:52,713 [INFO ]**(Log4jListener.java109) Task “groovyc” started.
14:19:52,729 [DEBUG]**(Log4jListener.java155) build.groovy omitted as build.class is up to date.
14:19:52,729 [INFO ]**(Log4jListener.java121) Task “groovyc” finished.

And, as many of you know, ant’s logging is one of the most important, if not only, way of troubleshooting ant script problems.

Here’s another attempt, but you can see I’m just using echo here:

def ant =new AntBuilder()

def srcDir          ="Source/groovy"
def libDir          ="Source/lib"
def classesDir      ="bin"

ant.sequential{
echo"Creating the output directories"
mkdir(dir:new File(classesDir))

echo"Defining the classpath"
path(id:"path"){
fileset(dir: libDir){
include(name:"**/*.jar")
}
pathelement(location: classesDir)
}

echo"Defining groovyc task"
taskdef(name:"groovyc", classname:"org.codehaus.groovy.ant.Groovyc", classpathref:"path")

echo"Compiling main classes"
groovyc(srcdir: srcDir, destdir: classesDir, classpathref:"path")

}

I know that with ant in the background, log4j logging must be there somewhere. I did some previous work using the ant api and was able to get basic logging going without too much trouble. This would be so much more useful if that were working.

Interesting groovy build script articles online:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

One Comment

  1. doc wrote:

    i bet you want to integrate groovy and ruby.

    Tuesday, July 31, 2007 at 8:47 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*