Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First another helper class:

Code Block
import groovy.xml.NamespaceBuilder

class MavenDependencyclass MavenDependency {
    static void require(params) {
        MavenDependencyHelper.getInstance().require(params)
    }
    static MavenDependencyHelper using(classLoader) {
        MavenDependencyHelper.getInstance(classLoader)
    }
}

private class MavenDependencyHelper {
    static private classLoader
    private ant = new AntBuilder(MavenDependencyHelper(classLoader) {
        this.classLoader = classLoader
    }

    static MavenDependencyHelper getInstance(classLoader) {
        return new MavenDependencyHelper(classLoader)
    }

    static privateMavenDependencyHelper intgetInstance() count{
= 0     static classLoader  return new MavenDependencyHelper(MavenDependencyHelper.classLoader)
    static }

    MavenDependencyHelper require(params) {
        def ant = new AntBuilder()
        def mvn = groovy.xml.NamespaceBuilder.newInstance(ant, 'antlib:org.apache.maven.artifact.ant')
        mvn.dependencies(filesetId:"artifacts$countartifact_${params.groupId}_${params.artifactId}_${params.version}") { dependency(params) }
        ant.fileScanner { fileset(refid:"artifacts$countartifact_${params.groupId}_${params.artifactId}_${params.version}") }.each {
            classLoader.addClasspath(it.toString())
        }
        count++this
    }
}

Now, here is the code we require to dynamically download the JFreeChart jars and add them to our classpath then run the script:

Code Block
// no jfreechart imports required (we'll find them programmatically)
import groovy.swing.SwingBuilder
import static javax.swing.WindowConstants.EXIT_ON_CLOSE
as
WC

def classLoader = Thread.currentThread().contextClassLoader
MavenDependency.classLoader
= classLoader

// load jars and add to classpath
def maven = MavenDependency.using(classLoader)
maven.require(groupId:'jfree', artifactId:'jfreechart', version:'1.0.5')
MavenDependencymaven.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)

...