Groovy and ant on steroids

I played around with groovy and groovy’s antbuilder. After a while I came to the follwing buildfile which resolves
dependencies in a quite general way:

new JavaLibArtifact(
        sources: "src/java",
        testsources: "src/test",
        libdir: "lib",
        testlibs: "/usr/share/java/junit.jar",
        testlibdir: "lib",
        targetdir: "target"
).build()
]]>

This is quite nice. If you want to look under the hood, here is the complete code:

class Artifact {
	ant = new AntBuilder()
	private uptodate = false

	public isUptodate() {
		return uptodate
	}

	public build() {
		if(uptodate) { return }
		buildOnce()
		uptodate = true
	}

}

class ArtifactSet extends Artifact {
	property artifacts

	public void buildOnce() {
		artifacts.each { it.build() }
	}

}

class JarArtifact extends Artifact {
	JavaCompilerArtifact classes
	String destfile

	public void buildOnce() {
		ant.jar(destfile: destfile,
		        basedir: classes.destdir)
	}
}

class JavaCompilerArtifact extends Artifact {
	property sources
	String   destdir
	property classpath
	private  classpaths

	void buildOnce() {
		ant.mkdir(dir: destdir)
		classpaths = ant.path()
		classpath.each() { 
			classpaths.add(it.path())
		}
		ant.javac(srcdir: sources,
		          classpath: classpaths,
			  destdir: destdir)
	}

	destdir() {
		build()
		return destdir
	}

	path(includeClassPath) {
		build()
		path = ant.path {
			pathelement(path: destdir)
		}
		if(!includeClassPath) {
			path.addExisting(classpaths)
		}

		return path
	}
}

class ClasspathArtifact extends Artifact {
	String libdir;
	private libs;
	private classpath;

	void buildOnce() {
		if(libdir) {
			classpath = ant.path {
				fileset(dir: "${libdir}") {
					include(name: "*.jar")
				}
			}
		}
		if(libs) {
			libs.each() {
				classpath.add(classpath.pathElement(it))
			}
		}
	}

	public path() {
		build()
		return classpath
	}

	public String toString() {
		build()
		return "Classpath: " + (libs ? libs.join(" ") + " " : "")
		                     + (libdir ? libdir + "/**" : "")
	}
}

class JUnitArtifact extends Artifact {
	property classes
	property testclasses
	String packages

	void buildOnce() {
		junit = ant.junit(printsummary: true, fork: true)
		testpath = junit.createClasspath()
		if(classes) {
			classes.each {
				testpath.add(it.path())
			}
		}
		if(testclasses) {
			testclasses.each {
				testpath.add(it.path())
			}
		}

		bt = junit.createBatchTest()
		testclasses.each() {
			bt.addFileSet(ant.fileset(dir: it.destdir) {
				include(name: "*")
			})
		}

		bt.elements().each() {
		}
		junit.setFork(true)
		junit.execute()
	}
}

class JavaLibArtifact extends Artifact {
	String sources
	String testsources
	String targetdir
	property libs
	String libdir
	property testlibs
	String testlibdir

	void buildOnce() {
		mainClasspath = new ClasspathArtifact(libdir: libdir,
		                                      libs: libs)
		testClasspath = new ClasspathArtifact(libdir: testlibdir,
		                                      libs: testlibs)

		mainClasses = new JavaCompilerArtifact(
				sources: sources,
				destdir: "${targetdir}/classes",
				classpath: mainClasspath)

		testClasses = new JavaCompilerArtifact(
		                       sources: testsources,
		                       destdir: "${targetdir}/test-classes",
		                       classpath: [mainClasses, mainClasspath,
	 	                       testClasspath])

		jar   = new JarArtifact(destfile: "${targetdir}/test.jar",
		                        classes: mainClasses)
		junit = new JUnitArtifact(classes: mainClasses, 
		                          testclasses: testClasses)

		new ArtifactSet(artifacts: [junit, jar]).build()
	}
}

new JavaLibArtifact(
	sources: "src/java",
	testsources: "src/test",
	libdir: "lib",
	testlibs: "/usr/share/java/junit.jar",
	testlibdir: "lib",
	targetdir: "target"
).build()
forum

{ Comments are closed! }

mail
conditions store
Bear
site-map