<?xml version='1.0'?>
<project name="Photos" default="help" basedir=".">

   <!--
      Give user a chance to override without editing this file
      (and without typing -D each time they run it).
   -->
  <property file="ant.properties" />
  <property file="${user.home}/ant.properties" />

  <property environment="env" />
  
  

  <!-- set global properties for this build -->

  <!-- Global javac properties -->
  <property name="failonerror" value="yes"/>
  <property name="listfiles" value="no"/>

  <!-- Directory for the java sources -->
  <property name="src" value="src"/>

  <!-- Root directory for compiled classes -->
  <property name="builddir" value="build"/>

  <!-- web root directory --> 
  <property name="webrootdir" value="WebRoot"/>
  
  <!-- Directory where java classes are stored -->
  <property name="classes" value="${webrootdir}/WEB-INF/classes"/>

  <!-- Target jar file name -->
  <property name="warname" value="Photos.war"/>
  
  <!-- Directories containing configuration files -->
  <property name="conf.dir" value="conf"/>
  <property name="spring.conf.dir" value="${conf.dir}/spring"/>
  <property name="property.conf.dir" value="${conf.dir}/properties"/>
  <property name="hibernate.conf.dir" value="${conf.dir}/hibernate"/>
  
  <property name="test.conf.dir" value="conf/test"/>
  <property name="test.spring.conf.dir" value="${test.conf.dir}/spring"/>
  <property name="test.property.conf.dir" value="${test.conf.dir}/properties"/>
  <property name="test.hibernate.conf.dir" value="${test.conf.dir}/hibernate"/>
  
  <!-- Directory where generated wars will be put -->
  <property name="wardir" value="build/deploy"/>

  <!-- Directory where generated documentation will be put -->
  <property name="docs" value="docs/javadoc"/>
  <property name="docs.pdf" value="docs/javadoc/docs.pdf"/>

  <!-- Directory where external jar files reside -->
  <property name="external" value="${webrootdir}/WEB-INF/lib"/>
  
  <!-- Directory where basic j2ee apis reside -->
  <property name="j2eelibs.dir" value="lib/j2ee"/>
  
  <!-- Directory where ant tasks are stored --> 
  <property name="antlibs.dir" value="lib/ant"/>
  
  <!-- Directory where test libraries are stored -->
  <property name="testlibs.dir" value="lib/test"/>

  <!-- Class name of the main class that must be run -->
  <property name="runclass" value="org.wamblee.Blah"/>

  <!-- Configuration for unit test -->

  <!-- Directory where test sources reside -->
  <property name="test.src" value="test"/>

  <!-- Directory where compiled test classes will be put -->
  <property name="test.classes" value="${builddir}/test"/>

  <!-- Use debug compilation or not -->
  <property name="debug" value="on"/>

  <!-- Directory where reports will reside -->
  <property name="reportdir" value="${builddir}/testreports"/>
  <property name="coveragedir" value="${builddir}/testcoverage"/>

  <!-- XML file for test coverage report -->
  <property name="coverreport" value="cl-cover.xml"/>

  <!-- XML file for unit test report -->
  <property name="unitreport" value="cl-unit.xml"/>
  
  <!-- Java source compliance level --> 
  <property name="compiler.source" value="1.5"/>
  
  <!-- Emma integration --> 
  <path id="emma.lib">
     <fileset dir="${antlibs.dir}" includes="emma*.jar"/>
  </path>
  <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
  <property name="emma.enabled" value="false"/>
  
  <target name="help">
      <echo>
          Available build targets are:
             classes:       Compile all sources from ${src} to ${classes}.
             war:           Create war file ${warname}
             build:         Make sure everything is compiled and build war
             rebuild:       Re-compile everything and build war
	     deploy:        Deploy the war file. 
             test:          Execute unit tests
             junit-reports: Execute tests and 
                            produce a report in the testreports directory. 
             javadoc:       Generate javadoc in ${docs}
             pdfdoc:        Generate javadoc in pdf format to ${docs.pdf}
             doccheck:      Check javadoc comment. 
             all:           Build everything
             run:           Run the application
             clean:         Cleanup
	     
	     Test coverage: 
	        Execute the above test targets with -Demma.enabled=true.
		
	        Emma can also be executed manually:
	     
	        emma:          Overwrites the production classes by their instrumented versions.
	        emma-reports:  Generates emma code coverage reports.
	        
	     Executing specific tests. 
	     
	        To execute specific test classes, specify -Dtest=PATTERN where PATTERN must match the 
	        unqualified test class name. For instance, -Dtest=Hibernate matches all testcase classes
	        that have Hibernate in their class name.  
      </echo>
  </target>

  <target name="init">
    <tstamp/>
    <mkdir dir="${classes}" />
    <mkdir dir="${docs}" />
    <mkdir dir="${test.classes}"/>
    <mkdir dir="${wardir}"/>
  </target>

  <path id="project.class.path">
    <!-- use the value of the ${classpath} property in the classpath -->
    
    <fileset dir="${external}">
      <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${j2eelibs.dir}">
      <include name="**/*.jar"/>
    </fileset>
  </path>
  
  <path id="project.test.class.path">
    <fileset dir="${testlibs.dir}">
      <include name="**/*.jar"/>
    </fileset>
    <path refid="project.class.path"/>
    <path refid="emma.lib"/>
    <pathelement path="${property.conf.dir}"/>
    <pathelement path="${spring.conf.dir}"/>
    <pathelement path="${hibernate.conf.dir}"/>
    
    <pathelement path="${test.property.conf.dir}"/>
    <pathelement path="${test.spring.conf.dir}"/>
    <pathelement path="${test.hibernate.conf.dir}"/>
    
  </path>
  
  

  <!-- ===========================================================================
       Main build targets
       =========================================================================== --> 
  <target name="classes" depends="init">

    <!-- run javac to compile the source files -->
    <javac srcdir="${src}" destdir="${classes}" debug="${debug}"
          source="${compiler.source}" failonerror="${failonerror}" listfiles="${listfiles}">
      <classpath refid="project.class.path"/>
    </javac>
    <!-- copy resources -->
    <copy todir="${classes}">
      <fileset dir="${src}">
        <exclude name="**/*.java"/>
      </fileset>
      <fileset dir="${spring.conf.dir}"/>
      <fileset dir="${property.conf.dir}"/>
      <fileset dir="${hibernate.conf.dir}"/>
    </copy>
   
  </target>

  <target name="war" depends="classes">
    <!-- make the war file -->
    <jar jarfile="${wardir}/${warname}" basedir="${webrootdir}/">
      <fileset dir=".">
        <include name="LICENSE"/>
        <include name="CHANGELOG"/>
      </fileset>
    </jar>
  </target>
  
  <!-- exploded deployment -->
  <target name="deploy" depends="classes">
    <property name="wardeploydir" value="${jboss.home}/server/${jboss.configuration}/deploy/${warname}"/>
    <delete dir="${wardeploydir}"/>
    <mkdir dir="${wardeploydir}"/>
    <copy todir="${wardeploydir}">
      <fileset dir="${webrootdir}"/>
    </copy>
  </target>

  <target name="build" depends="war">
  </target>

  <target name="rebuild" depends="clean,war">
  </target>

  <target name="javadoc" depends="init">
    <!-- create javadocs -->
    <javadoc packagenames="*"
      destdir="${docs}"
      author="true"
      version="true"
      use="true"
      private="yes"
      overview="${src}/architecture.html"
      windowtitle="SCJD. URLy bird assignment, SCJD"
      source="${compiler.source}"
      classpathref="project.class.path">
      <fileset dir="${src}">
        <include name="**/*.java"/>
      </fileset>
    </javadoc>
    <copy todir="${docs}">
      <fileset dir="${src}">
        <include name="doc-files/*"/>
      </fileset>
    </copy>
    
  </target>
  
  <path id="pdfdoclet.path">
    <fileset dir="${pdfdoclet.home}/jar">
       <include name="**/*.jar"/>
    </fileset>
  </path>

  <target name="pdfdoc" depends="init">
    <!-- create javadocs -->
    <javadoc packagenames="*"
      author="true"
      version="true"
      private="yes"
      source="${compiler.source}"
      doclet="com.tarsec.javadoc.pdfdoclet.PDFDoclet"
      docletpathref="pdfdoclet.path"
      additionalparam="-pdf ${docs.pdf}"
      classpathref="project.class.path">
      <fileset dir="${src}">
        <include name="**/*.java"/>
      </fileset>
    </javadoc>
  </target>


  <!-- create pdf documentation --> 
  <target name="doccheck" depends="init">
    <mkdir dir="${docs}/doccheck"/>
    <javadoc packagenames="*"
      destdir="${docs}/doccheck"
      author="true"
      version="true"
      doclet="com.sun.tools.doclets.doccheck.DocCheck"
      docletpath="${doccheck.home}/doccheck.jar"
      source="${compiler.source}"
      classpathref="project.class.path">
      <fileset dir="${src}">
        <include name="**/*.java"/>
      </fileset>
    </javadoc>
  </target>

  <target name="run" depends="war">
  <!-- run the class -->
    <java classname="${runclass}" fork="true">
      <!-- add a command line arg: <arg value="-h"/> -->
      <classpath>
        <!-- use the value of the ${classpath} property in the classpath -->
        <pathelement path="${classpath}"/>
        <!-- include all jar files  -->
        <fileset dir="${include}">
          <include name="**/*.jar"/>
        </fileset>
        <fileset dir=".">
            <include name="**/*.jar"/>
        </fileset>
      </classpath>
    </java>
  </target>

  <target name="cleandir">
  	<delete dir="${dir}"/>
  </target>

  <target name="clean" depends="base-test-clean">
    <antcall target="cleandir"><param name="dir" value="${classes}"/></antcall>
    <delete file="${jarname}"/>
    <antcall target="cleandir"><param name="dir" value="${docs}"/></antcall>
    <antcall target="cleandir"><param name="dir" value="${test.classes}"/></antcall>
    <antcall target="cleandir"><param name="dir" value="${wardir}"/></antcall>
    <delete>
      <fileset dir="." includes="**/ecoverage.ser"/>
    </delete>
    <antcall target="cleandir"><param name="dir" value="${builddir}"/></antcall>
  </target>

  <target name="all" depends="war,javadoc">
  </target>


  <!-- ===========================================================================
       Test targets 
       =========================================================================== -->

   <!-- compile test classes --> 
   
   <target name="testclasses" depends="classes">
      <mkdir dir="${test.classes}"/>
      <javac srcdir="${test.src}" destdir="${test.classes}" debug="${debug}"
             compiler="modern" failonerror="${failonerror}" listfiles="${listfiles}">
        <classpath>
          <path refid="project.test.class.path"/>
          <pathelement path="${classes}"/>
        </classpath>
      </javac>
  </target>
  
  <!--  Runs a main program found in the test source tree. 
    Properties:
    - my.test.program: class name of the program to run 
    - my.args: arguments to be passed to the program --> 
  <target name="runTestProgram" depends="testclasses">
    <java classname="${my.test.program}">
      <arg line="${my.args}"/>
      <classpath>
        <pathelement path="${test.classes}"/> 
        <pathelement path="${classes}"/>
        <path refid="project.test.class.path"/>
        <path refid="project.class.path"/>
      </classpath>
    </java>
  </target>

  <!-- test-base is a target mean to be used by other targets. 
       It expects one parameter names 'testedclasses' to indicate the directory
       where the compiled test classes reside. 
  -->
  
  <target name="base-test-clean">
    <delete file="coverage.ec"/>
    <delete file="coverage.em"/>
  </target>
  
  <target name="base-test-impl" depends="testclasses,base-test-clean">
    <mkdir dir="${reportdir}"/>
    <antcall target="emma"/>
    <junit printsummary="on" dir="." fork="yes" showoutput="yes">
      <batchtest todir="${reportdir}">
        <fileset dir="${test.classes}">
	    <include name="${testclassnames}" />
	    <!--  exclude inner classes --> 
	    <exclude name="**/*$$*.class"/>
        </fileset>
      </batchtest>
      <formatter type="xml"/>
      <classpath>
        <path refid="project.test.class.path"/>
        <pathelement path="${test.classes}"/>
        <pathelement path="${testedclasses}"/>
        <pathelement path="${testlibrary}"/>
        <pathelement path="${classes}"/>
      </classpath>
      <jvmarg value="-ea"/>
    </junit>
  </target>
  
  <target name="base-test-allclasses" unless="test">
    <echo>Testing all classes</echo>
    <antcall target="base-test-impl">
      <param name="testclassnames" value="**/*Test.class" />
    </antcall>
  </target>

  <target name="base-test-singleclass" if="test">
    <echo>Testing testcases that conform to the pattern *${test}*</echo>
    <antcall target="base-test-impl">
      <param name="testclassnames" value="**/*${test}*.class" />
    </antcall>
  </target>
  
  <target name="base-test">
    <antcall target="base-test-allclasses" />
    <antcall target="base-test-singleclass" />
  </target>

  <!-- Running tests without test coverage --> 
  
  <target name="test">
      <antcall target="base-test">
          <param name="testedclasses" value="${classes}"/>
      </antcall>
  </target>

  <!-- reporting target used by other targets. This should not depend on 
       the test target to avoid unnecessary duplicate runs of the tests --> 
       
  <target name="base-reports">
    <mkdir dir="${reportdir}/html"/>
    <mkdir dir="${reportdir}/html/unit"/>
    <junitreport todir="${reportdir}" tofile="${unitreport}">
      <fileset dir="${reportdir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report todir="${reportdir}/html/unit"/>
    </junitreport>
  </target>

  <!-- Reporting target for junit without the use of test coverage tools --> 
  
  <target name="junit-reports" depends="test">
    <antcall target="base-reports"/> 
    <antcall target="emma-reports"/>
  </target>
  
  <!-- Instrument classes using emma --> 
  
  <target name="emma">
    <emma enabled="${emma.enabled}">
      <instr instrpath="${classes}" mode="overwrite"/>
    </emma>
  </target>
  
  <target name="emma-reports" if="emma.enabled">
    <mkdir dir="${coveragedir}"/>
    <emma enabled="${emma.enabled}" >
      <report sourcepath="${src}">
        <!-- collect all EMMA data dumps (metadata and runtime): -->
        <infileset dir="." includes="*.em, *.ec" />

        <txt outfile="${coveragedir}/report.txt"/>
        <html outfile="${coveragedir}/index.html"/>
	<!-- sourcepath>
	  <dirset dir="${src}" >
            <include name="." /> 
          </dirset>
        </sourcepath -->
      </report>
    </emma>
  </target>
  
  <target name="forrest" description="runs Forrest">
    <property name="forrest.home" location="${env.FORREST_HOME}" />
    <property name="forrest.ant.home" location="${forrest.home}/tools/ant" />
    <java classname="org.apache.tools.ant.Main" fork="true" failonerror="true" maxmemory="128M">
      <classpath>
        <fileset dir="${forrest.ant.home}/lib">
          <include name="*.jar" />
        </fileset>
        <pathelement path="${java.home}/../lib/tools.jar" />
      </classpath>
      <sysproperty key="ant.home" value="${forrest.ant.home}" />
      <sysproperty key="forrest.home" value="${forrest.home}" />
      <sysproperty key="basedir" value="${basedir}" />
      <sysproperty key="java.endorsed.dirs" value="${forrest.home}/lib/endorsed" />
      <arg line="-f ${forrest.home}/main/forrest.build.xml" />
    </java>
  </target> 
  
  <target name="schemaexport">
    <antcall target="runTestProgram">
      <param name="my.test.program" value="org.wamblee.test.HibernateExporter"/>
      <param name="my.args" value="${builddir}/setup.sql ${hibernate.conf.dir}"/>
    </antcall>
    <echo>Generated SQL in ${builddir}/setup.sql</echo>
  </target>
  
  <target name="schemaupdate">
    <antcall target="runTestProgram">
      <param name="my.test.program" value="org.wamblee.test.HibernateUpdater"/>
      <param name="my.args" value="${hibernate.conf.dir}"/>
    </antcall>
  </target>

</project>



