<?xml version="1.0"?>
<project name="Simple JDBC statistics" default="all" basedir=".">
    <property file="build.properties"/>
    <property file="default.build.properties"/>
    <property file="default.properties"/>

    <path id="project.class.path" >
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
    </path>

    <path id="build.class.path">
        <pathelement location="${build.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${build.lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <path id="aux.class.path">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
    </path>

    <path id="findbugs.class.path">
        <pathelement location="${build.classes.dir}"/>
        <fileset dir="${build.lib.dir}">
            <include name="findbugs-1.3.9/**/*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">
        <pathelement location="${build.classes.dir}"/>
        <pathelement location="${test.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${build.lib.dir}">
            <include name="junit/**/*.jar"/>
        </fileset>
    </path>

    <path id="junit.class.path">
        <pathelement location="${build.classes.dir}"/>
        <pathelement location="${test.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${build.lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <path id="run.class.path">
        <pathelement location="${build.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
    </path>

    <path id="cobertura.class.path">
        <pathelement location="${test.classes.dir}"/>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/ext">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${build.lib.dir}/junit">
            <include name="**/*.jar"/>
        </fileset>
        <fileset dir="${build.lib.dir}/cobertura">
            <include name="cobertura.jar"/>
        </fileset>
        <pathelement location="${instrumented.classes.dir}"/>
    </path>

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

    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

    <!-- =================================================================== -->
    <!-- Clean                                                               -->
    <!-- =================================================================== -->
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <!-- =================================================================== -->
    <!-- Initialise                                                          -->
    <!-- =================================================================== -->
    <target name="init">

        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="build.class.path"
                 loaderRef="default"/>

        <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" classpathref="build.class.path"
                 loaderRef="default"/>

        <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="build.class.path"
                 loaderRef="default"/>

        <taskdef name="javancss" classname="javancss.JavancssAntTask" classpathref="build.class.path"
                 loaderRef="default"/>

    </target>

    <!-- =================================================================== -->
    <!-- Prepares the directory structure                                    -->
    <!-- =================================================================== -->
    <target name="prepare" depends="init">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${dist.dir}"/>
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${instrumented.classes.dir}"/>
        <mkdir dir="${test.classes.dir}"/>
        <mkdir dir="${build.dir}/testreports"/>
        <mkdir dir="${build.dir}/javadoc"/>
        <mkdir dir="${jar.dir}"/>
        <mkdir dir="${jarnodeb.dir}"/>
    </target>

    <!-- =================================================================== -->
    <!-- Generate javadoc                                                    -->
    <!-- =================================================================== -->
    <target name="javadoc">
        <javadoc destdir="${build.dir}/javadoc"
                 author="true"
                 version="true"
                 use="true"
                 windowtitle="${project.name}"
                 classpathref="build.class.path"
                 useexternalfile="false"
                 private="true"
                 stylesheetfile="javadoc.css">

            <fileset dir="${src.dir}" defaultexcludes="yes">
                <include name="**/*.java"/>
            </fileset>

            <doctitle>
                ${project.name}
            </doctitle>

            <bottom>
                All rights whatever.
            </bottom>

        </javadoc>
    </target>

    <!-- =================================================================== -->
    <!-- PMD                                                                 -->
    <!-- =================================================================== -->
    <target name="pmd" depends="prepare">
        <pmd shortFilenames="true" rulesetfiles="${config.dir}/pmd-ruleset.xml">
            <formatter type="html" toFile="${build.dir}/pmd_report.html"
                       linkPrefix="http://46.137.78.14:8080/job/VerySimpleTemplates/javadoc/"/>
            <formatter type="xml" toFile="${build.dir}/pmd.xml"
                       linkPrefix="http://46.137.78.14:8080/job/VerySimpleTemplates/javadoc/"/>
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

    <!-- =================================================================== -->
    <!-- PMD                                                                 -->
    <!-- =================================================================== -->
    <target name="cpd" depends="prepare">
        <cpd minimumTokenCount="100" format="xml" outputFile="${build.dir}/cpd.xml" ignoreidentifiers="true"
             ignoreliterals="true">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cpd>
    </target>

    <!-- =================================================================== -->
    <!-- FindBugs                                                            -->
    <!-- =================================================================== -->
    <target name="findbugs" depends="compile">
        <findbugs home="${build.lib.dir}/findbugs-1.3.9" output="xml" sourcepath="${src.dir}"
                  outputFile="${build.dir}/findbugs.xml"
                  reportLevel="medium"
                  excludeFilter="${config.dir}/findbug-exclude.xml"
                  jvmargs="-Xmx512m"
                  timeout="1200000">
            <classpath refid="build.class.path"/>
            <auxclasspath refid="aux.class.path"/>
            <class location="${build.classes.dir}"/>
        </findbugs>

    </target>

    <!-- =================================================================== -->
    <!-- NCSS                                                                -->
    <!-- =================================================================== -->
    <target name="ncss" depends="prepare">
        <javancss srcdir="${src.dir}"
                  includes="**/*.java"
                  generateReport="true"
                  outputfile="${build.dir}/javancss_metrics.xml"
                  format="xml">
            <classpath refid="project.class.path"/>
        </javancss>
    </target>

    <!-- =================================================================== -->
    <!-- Compile web sources                                                 -->
    <!-- =================================================================== -->
    <target name="compile" depends="prepare">
        <!-- generate.jsp, webdoclet,  -->
        <javac encoding="utf-8"
               destdir="${build.classes.dir}"
               classpathref="project.class.path"
               includes="**"
               debug="true"
               debuglevel="lines,vars,source"
               deprecation="on"
               optimize="on"
               verbose="false"
               target="1.5"
               source="1.5"
               includeantruntime="false">
            <src path="${src.dir}"/>
        </javac>
    </target>

    <target name="copynonjavafiles-src" depends="prepare">
        <!-- generate.jsp, webdoclet,  -->
        <!-- copy all non java class file to classes directorty -->
        <copy todir="${build.classes.dir}">
            <fileset dir="${src.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
        </copy>
        </target>
    <!-- =================================================================== -->
    <!-- Copy non java files                                                 -->
    <!-- =================================================================== -->
    <target name="copynonjavafiles" depends="prepare, copynonjavafiles-src">
        <!-- generate.jsp, webdoclet,  -->
        <!-- copy all non java class file to classes directorty -->
        <copy todir="${build.classes.dir}">
            <fileset dir="${test.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
        </copy>

        <copy todir="${instrumented.classes.dir}">
            <fileset dir="${src.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
            <fileset dir="${test.dir}">
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
        </copy>
    </target>

    <!-- =================================================================== -->
    <!-- Compile test sources                                                 -->
    <!-- =================================================================== -->
    <target name="compiletest" depends="prepare, compile, copynonjavafiles">
        <!-- generate.jsp, webdoclet,  -->
        <javac encoding="utf-8"
               destdir="${test.classes.dir}"
               includes="**"
               classpathref="test.class.path"
               debug="on"
               debuglevel="lines,vars,source"
               deprecation="on"
               optimize="on"
               verbose="false"
               target="1.5"
               source="1.5">
            <src path="${test.dir}"/>
        </javac>
    </target>

    <target name="cobertura-instrument" depends="compile">
        <delete file="${project.dir}/script/cobertura.ser"/>

        <cobertura-instrument todir="${instrumented.classes.dir}" datafile="${project.dir}/script/cobertura.ser">
            <fileset dir="${build.classes.dir}">
                <include name="**/*.class"/>
            </fileset>
        </cobertura-instrument>

        <copy todir="${instrumented.classes.dir}" overwrite="false">
            <fileset dir="${build.classes.dir}">
                <include name="**/*.class"/>
            </fileset>
        </copy>

    </target>

    <!-- =================================================================== -->
    <!--Deploy                                                               -->
    <!-- =================================================================== -->
    <!-- depends="cobertura-instrument, copynonjavafiles, compiletest" -->
    <target name="junit" depends="clean, copynonjavafiles, compiletest">

        <junit fork="yes" haltonfailure="no" forkmode="once">

            <jvmarg value="-ea"/>
            <jvmarg value="-noverify"/>
            <jvmarg value="-Xmx512m"/>
            <jvmarg value="-Dnet.sourceforge.cobertura.datafile=${project.dir}/script/cobertura.ser"/>

            <sysproperty key="net.sourceforge.cobertura.datafile" file="${project.dir}/script/cobertura.ser"/>

            <formatter type="xml" usefile="true"/>

            <classpath refid="junit.class.path"/>

            <batchtest fork="no" todir="${build.dir}/testreports">
                <fileset dir="${test.dir}">
                </fileset>
            </batchtest>
        </junit>

    </target>

    <target name="junit-cobertura" depends="clean, copynonjavafiles, compiletest, cobertura-instrument">

        <junit fork="yes" haltonfailure="no" forkmode="once" showoutput="false" printsummary="true">

            <jvmarg value="-ea"/>
            <jvmarg value="-noverify"/>
            <jvmarg value="-Xmx512m"/>
            <jvmarg value="-Dnet.sourceforge.cobertura.datafile=${project.dir}/script/cobertura.ser"/>

            <sysproperty key="net.sourceforge.cobertura.datafile" file="${project.dir}/script/cobertura.ser"/>

            <formatter type="xml" usefile="true"/>

            <classpath refid="cobertura.class.path"/>

            <batchtest fork="no" todir="${build.dir}/testreports">
                <fileset dir="${test.dir}">
                </fileset>
            </batchtest>
        </junit>

    </target>

    <!-- depends="cobertura-instrument, junit" -->
    <target name="cobertura-report" depends="prepare">

        <cobertura-report format="xml" destdir="${build.dir}" datafile="${project.dir}/script/cobertura.ser">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cobertura-report>

        <mkdir dir="${build.dir}/cobertura"/>

        <cobertura-report format="html" destdir="${build.dir}/cobertura" datafile="${project.dir}/script/cobertura.ser"
                          encoding="UTF-8">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cobertura-report>

    </target>

    <target name="jar" depends="compile, copynonjavafiles" description="Create jar distribution dir">

        <copy todir="${jar.dir}/lib">
            <fileset dir="${lib.dir}" includes="*.jar" excludes="h2*.jar"/>
            <fileset dir="${lib.dir}/ext" includes="*.jar"/>
        </copy>

        <manifestclasspath property="jar.class.path"
                           jarfile="${jar.dir}/${jar.name}.jar">
            <classpath refid="project.class.path"/>
        </manifestclasspath>

        <pathconvert property="manifest.classpath" pathsep=" ">
            <path refid="project.class.path"/>
            <mapper>
                <chainedmapper>
                    <flattenmapper/>
                    <globmapper from="*.jar" to="lib/*.jar"/>
                </chainedmapper>
            </mapper>
        </pathconvert>

        <jar destfile="${jar.dir}/${jar.name}.jar" basedir="${build.classes.dir}">
            <manifest>
                <attribute name="Class-Path" value="${manifest.classpath}"/>
            </manifest>
        </jar>

    </target>

    <target name="jar-nodeb" depends="compile, copynonjavafiles, jar" description="Create jar distribution dir">

        <unjar src="${jar.dir}/${jar.name}.jar"         dest="${jarnodeb.dir}" />
        <unjar src="${lib.dir}/slf4j-api-1.6.4.jar"     dest="${jarnodeb.dir}" />
        <unjar src="${lib.dir}/ext/simple-web-0.1.jar"  dest="${jarnodeb.dir}" />
        <unjar src="${lib.dir}/ext/vst-1.1.1.jar"       dest="${jarnodeb.dir}" />

        <jar destfile="${jar.dir}/${jar.name}-nodeb.jar" basedir="${jarnodeb.dir}">
            <manifest>
            </manifest>
        </jar>

    </target>

    <!-- =================================================================== -->
    <!-- Archive                                                            -->
    <!-- =================================================================== -->
    <target name="dist" depends="jar">

        <!-- create WAR file -->
        <zip destfile="${dist.dir}/${jar.name}.zip">
            <zipfileset dir="${jar.dir}" />
        </zip>

    </target>

    <!-- =================================================================== -->
    <!-- Hudson                                                              -->
    <!-- =================================================================== -->
    <target name="hudson" depends="clean, compile, junit, jar">
        <echo>Done.</echo>
    </target>

    <!-- =================================================================== -->
    <!-- Hudson                              jdepend                         -->
    <!-- =================================================================== -->
    <target name="all"
            depends="init, prepare, javadoc, ncss, findbugs, pmd, cpd, jar">
        <echo>Done.</echo>
    </target>

    <!-- =================================================================== -->
    <!-- Archive                                                             -->
    <!-- =================================================================== -->
    <target name="archive" depends="clean, init">

        <mkdir dir="${build.dir}"/>

        <!-- create WAR file -->
        <zip destfile="${build.dir}/PrevaylerReferenceArchive.zip">
            <zipfileset dir="${src.dir}" prefix="src/"/>
            <zipfileset dir="${project.dir}/script" prefix="script/"/>
            <zipfileset dir="${project.dir}/config" prefix="config/"/>
        </zip>

    </target>

</project>
