<?xml version='1.0'?>
<!--
Apache Ant build.xml template
-->
<project name="project" default="dist" basedir=".">

	<!-- define basedir - used to be able to import this file from outside -->
	<dirname property="boot.basedir" file="${ant.file.${ant.project.name}}"/>
    <property name="jarName" value="${ant.project.name}.jar"/>

    <property name="src.dir" location="${boot.basedir}/src"/>
	<property name="target.dir" location="${boot.basedir}/target"/>
	<property name="target.classes" location="${target.dir}/classes"/>
    <property name="target.doc" value="${target.dir}/doc"/>
	
	<property name="tests.dir" location="${target.dir}/tests"/>
	<property name="tests.src" location="${boot.basedir}/tests"/>
	<property name="tests.classes" location="${tests.dir}/classes"/>
	<property name="tests.reports" location="${tests.dir}/reports"/>
	
	<property name="lib.dir" location="${boot.basedir}/lib"/>
    <property name="lib.runtime.dir" location="${lib.dir}/runtime"/>
	<property name="lib.compile.dir" location="${lib.dir}/devel"/>

	<!-- library classpaths -->
	<path id="lib-runtime">
		<fileset dir="${lib.runtime.dir}" includes="**/*.jar" />
	</path>	
	<path id="lib-compile">
		<fileset dir="${lib.compile.dir}" includes="**/*.jar" />
	</path>
	
    <path id="fullclasspath">
        <pathelement location="${target.classes}"/>
        <path refid="lib-compile"/>
    	<path refid="lib-runtime"/>
    </path>
	
	<target name="pre_init">		
		<mkdir dir="${target.dir}"/>		
		<mkdir dir="${target.classes}"/>
		<mkdir dir="${tests.dir}"/>
		<mkdir dir="${tests.classes}"/>
		<mkdir dir="${tests.reports}"/>
	</target>

	<!-- Compile target. Compiles all java-files to buildDir. -->
    <target name="compile" depends="pre_init" description="Compile all source files">
        <javac srcdir="${src.dir}" destdir="${target.classes}" classpathref="fullclasspath" debug="on" debuglevel="lines,source" />
    	<!-- copy everything that is not a .java file to the classes target directory -->
  		<copy todir="${target.classes}">
   		    <fileset dir="${src.dir}">
   		      <exclude name="**/*.java"/>
   		    </fileset>
   		</copy>    	
    </target>

	<target name="dist-one" depends="clean, compile" description="Build library and pack jar file">
		<!-- pack everything together -->
		<jar destfile="${target.dir}/${jarName}" 
			index="true"
			filesetmanifest="mergewithoutmain">
			<manifest>
			      <attribute name="Main-Class" value="${main.class}" />
			</manifest>
		    <fileset dir="${target.classes}"/>	
			<zipgroupfileset dir="${lib.runtime.dir}" includes="*.jar"/>
		</jar>
	</target>
	
    <target name="dist" depends="clean, compile" description="Build library and pack jar file">
    	   <mkdir dir="${target.dir}/lib"/>       
	        <!-- copy jar from runtime lib directory -->
	        <copy todir="${target.dir}/lib" overwrite="false">
	            <fileset dir="${lib.runtime.dir}">
	                <include name="**/*.jar"/>              
	            </fileset>
	        </copy>
	        <!-- pack everything together -->
	        <jar destfile="${target.dir}/${jarName}" index="true" >
	            <fileset dir="${target.classes}"/>            
	        </jar>
	 </target>

	
	<target name="doc" depends="pre_init" description="Create Javadoc">		
		<mkdir dir="${target.doc}/api"/>
		<javadoc packagenames="*"
		           sourcepath="${src.dir}"
				   classpathref="fullclasspath"
		           defaultexcludes="yes"
		           destdir="${target.doc}/api"
		           author="true"
		           version="true"
		           use="true"
		           windowtitle="Bioinf Commons Library API">			
		    <doctitle><![CDATA[<h1>API Documentation</h1>]]></doctitle>		    
		    <tag name="todo" scope="all" description="To do:"/>
		    <link href="http://java.sun.com/j2se/1.5.0/docs/api/"/>
		  </javadoc>
	</target>
	
	<!-- tests -->
    <target name="compile-tests" depends="compile" description="Compile all source files">
        <javac srcdir="${tests.src}" destdir="${tests.classes}" classpathref="fullclasspath" debug="on" debuglevel="lines,source" />
    	<!-- copy everything that is not a .java file to the classes target directory -->
  		<copy todir="${tests.classes}">
   		    <fileset dir="${tests.src}">
   		      <exclude name="**/*.java"/>
   		    </fileset>
   		</copy>    	
    </target> 

	<target name="test" depends="compile-tests" description="Run JUnit Tests">
		<!-- dependes on ant >= 1.7 -->
		<!-- define junit task using our ant-junit-1.7.0.jar and junit-4.4.jar -->		
		<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
		      <classpath>
		        <pathelement location="${lib.compile.dir}/junit-4.5.jar"/>
		        <pathelement location="${lib.compile.dir}/ant-junit-1.7.0.jar"/>
		      </classpath>
		</taskdef>

		<junit printsummary="yes" haltonfailure="false" fork="true">
			<classpath>
				<path refid="fullclasspath" />
				<pathelement path="${tests.classes}"/>
			</classpath>
			<formatter type="xml"/>
			<batchtest fork="yes" todir="${tests.reports}" >						
			    <fileset dir="${tests.src}">
			      <include name="**/*Test*.java"/>
			    </fileset>
		  	</batchtest>
		</junit>
	</target>	
	
	<target name="html-reports" depends="test" description="Creates HTML from test reports">
		<!-- dependes on ant >= 1.7 -->
		<!-- define junit task using our ant-junit-1.7.0.jar and junit-4.4.jar -->		
		<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
		      <classpath>
		        <pathelement location="${lib.compile.dir}/junit-4.5.jar"/>
		        <pathelement location="${lib.compile.dir}/ant-junit-1.7.0.jar"/>
		      </classpath>
		</taskdef>

		<mkdir dir="${tests.reports}/html"/>
		<junitreport todir="${tests.reports}/html">
		    <fileset dir="${tests.reports}">
		      <include name="TEST-*.xml" />
		    </fileset>
		    <report todir="${tests.reports}" />
		 </junitreport>
	</target>

    <!-- Clean target. Removes the buildDir. (class files) -->
    <target name="clean" description="Clean">
        <delete dir="${target.dir}"/>    	
    </target>
</project>
