SDA SE Wiki

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>
<path id="project.classpath">
	<path refid="mainproject.classpath" />
	<!-- You could include libraries via: -->
    <fileset dir="${lib.dir}">
      <include name="**/*.jar"/>
    </fileset>		
</path>
<target name="init" description="Create the directories for compiled files.">
	<mkdir dir="${build.dir}" />
	<mkdir dir="${test-results.dir}" />
</target>
<target name="clean" description="Remove the directories for compiled files.">
	<delete dir="${build.dir}" />
	<delete dir="${test-results.dir}" />
</target>

<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"/>
   </propertyfile>
</target>
<target depends="init-shortresult,init" name="build-src" description="Build the classes in the src folder.">
	<echo message="${ant.project.name}: ${ant.file}" level="info" />
	<javac destdir="${build.dir}">
		<src path="src" />
		<classpath>
		    <pathelement location="junit.jar"/>
		</classpath>
		<classpath refid="project.classpath" />
	</javac>
     <!-- well, compiling seemed to work :) -->
   <propertyfile file="${resultfile}">
	    <entry key="BUILD" type="string" value="OK"/>
	   </propertyfile>
</target>
<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}">
		<classpath>
			<pathelement location="${build.dir}" />
		</classpath>
		<classpath refid="project.classpath" />
		<formatter usefile="false" type="brief" />
		<formatter type="xml" />
		<batchtest todir="${test-results.dir}">
			<fileset includes="**/*Test.class" dir="${build.dir}" />
		</batchtest>
	</junit>
</target>
<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"/>
	   </propertyfile>
</target>
<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>
<target depends="build-src" name="jar" description="Build distributables.">
	<mkdir dir="${dist.dir}" />
	<jar basedir="${build.dir}" destfile="${dist.dir}/${ant.project.name}.jar" />
</target> 

</project>

</Code>

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, © 2023