<?xml version="1.0" encoding="UTF-8"?>
<!--
	$Id: build.xml,v 1.5 2006/03/12 20:06:27 hom Exp $
	@author Pieter van den Hombergh, Ferd van Odenhoven
	Fontys Technische Hogeschool Venlo
	
	Generic build.xml for java projects.
	Can be used with a build.properties file
	that can (re)define all properties but most likely
	appmain and appname. See customisation.
	
	Used conventions: Application classes and packages 
	are stored under .../src.
	Compilation output is directed to .../classes
	Test classes are in a tree under .../test with the 
	same package/dir struct as under .../src
	The testpackages are compiled into the testclasses directory.
	
-->
<project name="genericbuild" default="compile" basedir=".">
	<!-- most, if not all properties can be set in build.properties 
	Must read them before any override. -->
		<property file="build.properties"/>
<!-- 
	Customisation:
	First rule: use the build.properties file. That is easier
	to maintain and understand.
	define 
	appname=<name of your applicatione>
	appmain=<the fully qualified classname with the starting main() >
	Example 
	appname=shoppingcart
	appmain=shop.Main
	
	If you think you can do a better job:
	Customisations should be done by setting different values to the 
	project name and possibly the location of the class containing the 
	main() method.	
	Other customisation could have to be done in the external libraries 
	on the classpath.
	The settings below are sorted with the most likely candidates
	for customisation at the top.
-->
	<!-- customize for application name -->
	<property name="appname"	
		description="name of the application and jar file"
		value="shoppingcart"
	/>
	<!-- customize for the fully qualified 
	     Classname with THE main() method -->
	<property name="appmain"
		description="class with the main method to start the application" 
		value="shop.Main"
	/>
	
	<!-- Location of the system jar files. Set for /usr/share/java
	    which is the default in SuSE linux variants and maybe more. -->
	<property name="javalib"   
		description="Location of the jar libs. Default is set for linux"
		location="/usr/share/java"
	/>
	
	<property name="src" 	description="source tree of application"  	
		location="src"
	/>
	<property name="test" 	description="source tree of test classes"	
		location="test"			
	/>
	<property name="build" 	description="output tree for compilation"
		location="classes"		
	/>
	<property name="testbuild" 	
		description="output for compilation of test classes"
		location="testclasses"		
	/>
        <property name="test.xml.dir"
                description="output the xml test reports"
                location="test-results"/>

	<!-- Maybe you need additional jars in other place add them
	  like the fileset below, preferabbly by first setting a variable  -->
	<path id="project.classpath">
		<pathelement location="${build}"/>
		<pathelement location="${testbuild}"/>
		<fileset dir="${javalib}">
			<include name="**/*.jar"/>
		</fileset>
	</path>

	<!-- No customisations needed below  this comment if you stick
	  the sane conventions in the java world like below -->

	
	<property name="jar" 	
		description="output tree for application jar"
		location="jar"		
	/>
	
	<property name="jarfile" 	
		description="full pathname of jar file"
		location="${jar}/${appname}.jar"		
	/>
	
	<property name="zip.filename"
		description="The name of the zip file"
		value="${appname}.zip"/>
	
	<property name="publish.dir"
		description="dir the result files are published"
		location="/tmp/${appname}"
	/>
	
	<!-- Get the work done:
	  These are the targets to be executed.
	 -->
	
	<target name="jar" 
		depends="compile" 
		description="Compile the sources and makes an executable jar ">
		<mkdir dir="${jar}" />
		<jar jarfile="${jarfile}" basedir="${build}">
			<manifest>
				<attribute name="Main-Class" value="${appmain}"/>
			</manifest>
		</jar>
		<echo message="appmain=${appmain}, appname=${appname}"/>
		<echo message="run application with command on next line"/>
		<echo message="java -jar ${jarfile}"/>
	</target>

	<target name="run"
		depends="jar"
		description="Run the appication from the jar">
		<!-- run in separate VM -->
		<java jar="${jarfile}"
			fork = "true"
			failonerror="true">
			<classpath refid="project.classpath"/>
		</java>
	</target>
	
	<target name="initbuild" 
		description="Creates the build folders">
		<property file="build.properties"/>
		<echo message="Creating Build Folder"/>
		<mkdir dir="${build}" />
		<mkdir dir="${testbuild}" />
	</target>

	<target name="compile" depends="initbuild" 
		description="Compile the sources without tests ">
		<javac srcdir="${src}" debug="false" destdir="${build}" />
	</target>

	<target name="compiletests" depends="compile,initbuild" 
		description="Compiles the sources with tests ">
		<javac srcdir="test" debug="false" destdir="${testbuild}">
			<classpath refid="project.classpath"/>
		</javac>
	</target>

	<target name="unittest" depends="compiletests" 
		description="Compiles the sources with tests and runs the tests">
                <delete dir="${test.xml.dir}"/>
                <mkdir dir="${test.xml.dir}"/>
		<junit haltonfailure="true">
			<classpath refid="project.classpath"/>
			<formatter type="brief" usefile="false"/>
                        <formatter type="xml"/>
			<batchtest todir="${test.xml.dir}">
				<fileset dir="${testbuild}" includes="**/*Test.class"  />
			</batchtest>
		</junit>
	</target>

	<target name="coveragetest" 
		description="Execute the coverage tests"
		depends="compiletests">
	<!-- the *CoverageTest.java files are assumed to live 
	  in the test tree, so compilation will have been 
	  done in compiletest -->
		<junit haltonfailure="false">
			<classpath refid="project.classpath"/>
			<formatter type="brief" usefile="false"/>
			<batchtest>
				<fileset dir="${testbuild}"
	          includes="**/*TestCoverage.class"  />
			</batchtest>
		</junit>
	</target>
	
    <target name="javadoc"
    	description="Build the html api doc with javadoc">
         <javadoc access="private" 
			author="true" 
         	classpath="." 
         	destdir="api" 
         	nodeprecated="false" 
         	nodeprecatedlist="false" 
         	noindex="false" 
         	nonavbar="false" 
         	notree="false" 
        	packagenames="**/*"
         	source="1.5" 
         	sourcepath="src" 
         	splitindex="true" 
         	use="true" version="true">
			<link href="http://java.sun.com/j2se/1.5.0/docs/api"/>
		</javadoc>
	</target>
	
	<target name="publish"
		description="Bundle the whole stuff and publish to directory"
		depends="jar,javadoc">
		<copy todir="${publish.dir}/api">
			<fileset dir="api/"/>
		</copy>
		<copy todir="${publish.dir}">
			<fileset file="jar/*.jar"/>
		</copy>
		<zip destfile="${publish.dir}/${zip.filename}">
			<fileset dir="../${appname}"
				excludes="**/CVS,${zip.excludes}"/>
		</zip>
	</target>
	
	<target name="clean" 
		description="Deletes the folders with classes and jar (if existing)">
		<delete dir="${build}"/>
		<delete dir="${testbuild}"/>
		<delete dir="${jar}" />
	</target>

</project>
<!-- eof build.xml -->
