<project name="apgdiff" default="jar" basedir=".">
    <property name="name" value="Another PostgreSQL Diff Tool"/>
    <property name="version" value="2.5"/>
    <property name="build" value="build"/>
    <property name="dist" value="dist"/>
    <property name="build.output" value="${build}/classes"/>
    <property name="dist.javadoc" value="${dist}/javadoc"/>
    <property name="build.final.name" value="${ant.project.name}-${version}"/>
    <property name="lib" value="lib"/>
    <property name="test.reports" value="${build}/test-reports"/>
    <property name="test.output" value="${build}/test-classes"/>
    <property name="src" value="src/main/java"/>
    <property name="src.resources" value="src/main/resources"/>
    <property name="test.src" value="src/test/java"/>
    <property name="test.resources.src" value="src/test/resources"/>
    <property name="source" value="1.5"/>
    <property name="target" value="1.5"/>
    <property name="main.class" value="cz.startnet.utils.pgdiff.Main"/>

    <path id="build.classpath">
        <fileset dir="${lib}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <target name="init" description="Initializes build environment">
        <tstamp/>
    </target>

    <target name="clean" description="Clean the output directory">
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
        <delete dir="${lib}"/>
    </target>

    <target name="compile" depends="init,get-deps"
            description="Compile the code">
        <mkdir dir="${build.output}"/>
        <javac destdir="${build.output}" srcdir="${src}" source="${source}"
               target="${target}" debug="true" deprecation="true"
               optimize="false"/>
        <copy todir="${build.output}">
            <fileset dir="${src.resources}"/>
        </copy>
     </target>

    <target name="jar" depends="compile" description="Clean the JAR">
        <mkdir dir="${dist}"/>
        <jar jarfile="${dist}/${build.final.name}.jar"
             basedir="${build.output}">
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
    </target>

    <target name="compile-tests" depends="compile"
            description="Compile the test code">
        <mkdir dir="${test.output}"/>
        <javac destdir="${test.output}" srcdir="${test.src}" source="${source}"
               target="${target}" debug="true" deprecation="true"
               optimize="false">
            <classpath>
                <path refid="build.classpath"/>
                <pathelement location="${build.output}"/>
            </classpath>
        </javac>
        <copy todir="${test.output}">
            <fileset dir="${test.resources.src}"/>
        </copy>
    </target>

    <target name="test" depends="compile-tests"
            description="Run the test cases">
        <mkdir dir="${test.reports}"/>
        <junit printSummary="yes" haltonerror="true" haltonfailure="true"
               fork="false" dir=".">
            <sysproperty key="basedir" value="."/>
            <formatter type="plain" usefile="false"/>
            <classpath>
                <path refid="build.classpath"/>
                <pathelement location="${build.output}"/>
                <pathelement location="${test.output}"/>
            </classpath>
            <batchtest todir="${test.reports}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="get-deps" description="Download all dependencies"
            unless="noget">
        <mkdir dir="${lib}"/>
        <get src="http://central.maven.org/maven2/junit/junit/4.8.1/junit-4.8.1.jar"
             dest="${lib}/junit.jar"/>
	<get src="http://central.maven.org/maven2/org/hamcrest/hamcrest-all/1.1/hamcrest-all-1.1.jar"
	     dest="${lib}/hamcrest-all.jar"/>
    </target>

    <target name="javadoc" description="Generates javadoc">
        <delete dir="doc"/>
        <mkdir dir="doc"/>
        <javadoc packagenames="cz.*" sourcepath="${src}"
                 destdir="${dist.javadoc}" classpathref="build.classpath"
                 author="true" version="true" private="false" use="true"
                 splitindex="true" windowtitle="${name} ${version}"
                 doctitle="${name} ${version}"/>
    </target>

</project>
