Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools

Code Templates

Generic Buildfile

This is a simple buildfile, which compiles the java code and runs the junit tests. The result is written in a very short form to results.txt.

<code xml|Hbuild.xml> <?xml version=“1.0”?>

<project basedir=“.” name=“ProjectNameGoesHere” default=“all”>

<target depends="clean,init,build-src,run-tests,jar" name="all" />
<property name="resultfile" location="results.txt" />
<property name="build.dir" location="target/src-classes" />
<property name="test-results.dir" location="target/test-results" />
<property name="dist.dir" location="dist" />
<property name="lib.dir" location="lib" />
<path id="mainproject.classpath">
	<pathelement location="${build.dir}" />
<path id="project.classpath">
	<path refid="mainproject.classpath" />
	<!-- You could include libraries via: -->
    <fileset dir="${lib.dir}">
      <include name="**/*.jar"/>
<target name="init" description="Create the directories for compiled files.">
	<mkdir dir="${build.dir}" />
	<mkdir dir="${test-results.dir}" />
<target name="clean" description="Remove the directories for compiled files.">
	<delete dir="${build.dir}" />
	<delete dir="${test-results.dir}" />

<target name="init-shortresult" description="Initialize the results.txt file">
	<!-- assume, nothing would work -->
	<propertyfile file="${resultfile}">
			<entry key="BUILD" type="string" value="FAILED"/>
			<entry key="TESTS" type="string" value="FAILED"/>
<target depends="init-shortresult,init" name="build-src" description="Build the classes in the src folder.">
	<echo message="${}: ${ant.file}" level="info" />
	<javac destdir="${build.dir}">
		<src path="src" />
		    <pathelement location="junit.jar"/>
		<classpath refid="project.classpath" />
     <!-- well, compiling seemed to work :) -->
   <propertyfile file="${resultfile}">
	    <entry key="BUILD" type="string" value="OK"/>
<target name="real-run-test"  description="Target which runs the junit tests.">
	<junit showoutput="yes" fork="yes" failureproperty="junit.error" printsummary="yes" dir="${build.dir}">
			<pathelement location="${build.dir}" />
		<classpath refid="project.classpath" />
		<formatter usefile="false" type="brief" />
		<formatter type="xml" />
		<batchtest todir="${test-results.dir}">
			<fileset includes="**/*Test.class" dir="${build.dir}" />
<target name="set-test-ok" unless="junit.error" description="Sets the JUnit result to OK.">
     <!-- well, junits seemed to work :) -->
	    <propertyfile file="${resultfile}">
	    <entry key="TESTS" type="string" value="OK"/>
<target name="run-tests" depends="real-run-test,set-test-ok" description="Run the junit tests and publish the results.">
	<fail message="JUnit test(s) failed!" if="junit.error" />
<target depends="build-src" name="jar" description="Build distributables.">
	<mkdir dir="${dist.dir}" />
	<jar basedir="${build.dir}" destfile="${dist.dir}/${}.jar" />



To run this in Eclipse, you have to add the junit.jar to the ant classpath in the general preferences:

teaching/labs/xp/2007b/code_templates.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2021