...
This makes our code look simpler for this example but be careful with this approach though as you need to avoid name clashes. You might find it easier to define helper methods to hide away the cumbersome namespace notation as shown below. First a helper class:
| Code Block |
|---|
class AntLibHelper {
def namespace, ant
Object invokeMethod(String name, Object params) {
ant."antlib:$namespace:$name"(*params)
}
}
|
Now The preferred way is to use the NamespaceBuilder. Using this, our code becomes:
| Code Block |
|---|
import groovy.xml.NamespaceBuilder def ant = new AntBuilder() def antunit = new AntLibHelperNamespaceBuilder.newInstance(ant:ant, namespace'antlib:'org.apache.ant.antunit') def destfile = 'copytest1.tmp' antunit.assertFileDoesntExist(file:destfile) ant.copy(file:'src/antunit.groovy', tofile:destfile) antunit.assertFileExists(file:destfile) ant.delete(file:destfile) antunit.assertFileDoesntExist(file:destfile) |
...
Here is how you could use these tasks to download some required jars into your local maven repository cache (~/.m2 directory).
| Code Block |
|---|
import groovy.xml.NamespaceBuilder def ant = new AntBuilder() items = [[groupId:'jfree', artifactId:'jfreechart', version:'1.0.5'], [groupId:'jfree', artifactId:'jcommon', version:'1.0.9']] def mvn = new AntLibHelperNamespaceBuilder.newInstance(ant:ant, namespace'antlib:'org.apache.maven.artifact.ant') // download artifacts mvn.dependencies(filesetId:'artifacts') { items.each { dependency(it) } } // print out what we downloaded ant.fileScanner { fileset(refid:'artifacts') }.each { println it } |
...
First another helper class:
| Code Block |
|---|
import groovy.xml.NamespaceBuilder class MavenDependency { static private ant = new AntBuilder() static private int count = 0 static classLoader static require(params) { def mvn = new AntLibHelperNamespaceBuilder.newInstance(ant:ant, namespace'antlib:'org.apache.maven.artifact.ant') mvn.dependencies(filesetId:"artifacts$count") { dependency(params) } ant.fileScanner { fileset(refid:"artifacts$count") }.each { classLoader.addClasspath(it.toString()) } count++ } } |
...
| Code Block |
|---|
// no jfreechart imports required (we'll find them programmatically) import groovy.swing.SwingBuilder import javax.swing.WindowConstants as WC def classLoader = Thread.currentThread().contextClassLoader MavenDependency.classLoader = classLoader // load jars and add to claspathclasspath MavenDependency.require(groupId:'jfree', artifactId:'jfreechart', version:'1.0.5') MavenDependency.require(groupId:'jfree', artifactId:'jcommon', version:'1.0.9') // define used classes/instances programmatically def factoryClass = classLoader.loadClass('org.jfree.chart.ChartFactory', true) def orientationClass = classLoader.loadClass('org.jfree.chart.plot.PlotOrientation', true) def dataset = classLoader.loadClass('org.jfree.data.category.DefaultCategoryDataset', true).newInstance() // normal code below here dataset.addValue 150, "no.1", "Jan" dataset.addValue 210, "no.1", "Feb" dataset.addValue 390, "no.1", "Mar" dataset.addValue 300, "no.2", "Jan" dataset.addValue 400, "no.2", "Feb" dataset.addValue 200, "no.2", "Mar" def labels = ["Bugs", "Month", "Count"] def options = [true, true, true] def chart = factoryClass.createLineChart(*labels, dataset, orientationClass.VERTICAL, *options) def swing = new SwingBuilder() def frame = swing.frame(title:'Groovy LineChart', defaultCloseOperation:WC.EXIT_ON_CLOSE) { panel(id:'canvas') { rigidArea(width:400, height:400) } } frame.pack() frame.show() chart.draw(swing.canvas.graphics, swing.canvas.bounds) |
...
We can also download jars using Ivy. In this case we use MarkupBuilder to build an XML file that the Ivy retrieve task will use:
| Code Block |
|---|
import groovy.xml.NamespaceBuilder def ant = new AntBuilder() def ivyfile = 'ivy.xml' // default file used by Ivy ant.delete(file:ivyfile, quiet:true) new File(ivyfile).withWriter { writer -> def builder = new groovy.xml.MarkupBuilder(writer) builder.'ivy-module'(version:'1.0') { info(organisation:"codehaus", module:"GroovyExamples") dependencies { dependency(org:'jfree', name:'jfreechart', rev:'1.0.5') dependency(org:'jfree', name:'jcommon', rev:'1.0.9') } } } def ivy = new AntLibHelperNamespaceBuilder.newInstance(ant:ant, namespace'antlib:'org.apache.ivy.ant') ivy.retrieve() ivy.report(toDir:'reports') // optional |
...