<?xml version="1.0"?>
<project name="mywebwork2" default="default">

  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <!--                   compile settings                -->
  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <property name="build.debug" value="on"/>

  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <!--                   directories                     -->
  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="build"/>
  <property name="build.classes.dir" value="${build.dir}/classes"/>
  <property name="build.jspc.java.dir" value="${build.dir}/jspc/java"/>
  <property name="build.jspc.classes.dir" value="${build.dir}/jspc/classes"/>
  <property name="dist.dir" value="dist"/>
  <property name="lib.dir" value="lib"/>

  <property name="tomcat.home.dir" value="C:\Program Files\Apache Group\Tomcat 4.1"/>
  <path id="jasper.classpath">
    <fileset dir="${tomcat.home.dir}/common/lib">
      <include name="**/*.jar"/>
    </fileset>
    <pathelement path="${java.class.path}"/>
  </path>

  <!-- javadoc properties -->
  <property name="javadoc.dir" value="doc"/>
  <property name="javadoc.packages" value="com.mammals.dogs.*"/>

  <!-- junit properties -->
  <property name="test.src.dir"	value="src"	/>
  <property name="test.reports.dir" value="reports"/>

  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <!--                     datatypes                     -->
  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <path id="project.classpath">
    <pathelement location="${build.classes.dir}"/>
    <fileset dir="${lib.dir}">
      <include name="**/*.jar" />
      <include name="**/*.zip" />
    </fileset>
  </path>

  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <!--                  targets                          -->
  <!-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
  <target name="default"
          depends="dist"
          description="default: build everything"
  />

  <target name="all"
          depends="test,dist"
          description="build and test everything"
  />

  <target name="clean" description="remove all built files">
    <delete dir="${javadoc.dir}" />
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <target name="dist"
          depends="compile"
          description="create distributables (jars etc)">
    <mkdir dir="${dist.dir}" />
    <war destfile="${dist.dir}/mywebwork2.war"
         webxml="${src.dir}/metadata/web.xml">
      <fileset dir="${src.dir}/html"/>
      <fileset dir="${src.dir}/jsp"/>
      <lib dir="${lib.dir}"/>
      <classes dir="${build.classes.dir}"/>
      <classes dir="${src.dir}/metadata">
      	<include name="xwork.xml"/>
      	<include name="components.xml"/>
      	<include name="webwork.properties"/>
      </classes>
      <webinf dir="${src.dir}/metadata">
      	<include name="webwork.tld"/>
      </webinf>
    </war>
  </target>

   <target name="deploy"
           depends="dist"
           description="deploy wars into tomcat">
           
           <delete dir="${tomcat.home.dir}/work/Standalone/localhost/mywebwork2" />
           <delete dir="${tomcat.home.dir}/webapps/mywebwork2" />
           <copy file="${dist.dir}/mywebwork2.war" todir="${tomcat.home.dir}/webapps"/>
   </target>

  <target name="unwar"
          depends="dist"
          description="unpacks web archive">
    <property name="war.expanded.dir" value="${build.dir}/unpacked-warfile"/>
    <mkdir dir="${war.expanded.dir}"/>
    <unwar src="${dist.dir}/mywebwork2.war" dest="${war.expanded.dir}"/>
  </target>

  <target name="compile-jsp"
          depends="unwar"
          description="pre-compiles the jsp pages">
    <mkdir dir="${build.jspc.classes.dir}"/>
    <mkdir dir="${build.jspc.java.dir}"/>

    <jspc srcdir="${war.expanded.dir}" destdir="${build.jspc.java.dir}">
      <include name="**/*.jsp"/>
      <classpath refid="jasper.classpath"/>
    </jspc>

    <javac srcdir="${build.jspc.java.dir}"
           destdir="${build.jspc.classes.dir}"
           debug="${build.debug}"
           deprecation="on">
      <classpath>
        <path location="${war.expanded.dir}/WEB-INF/classes"/>
        <fileset dir="${war.expanded.dir}/WEB-INF/lib">
          <include name="**/*.jar"/>
        </fileset>
        <path refid="jasper.classpath"/>
      </classpath>
    </javac>
  </target>

  <target name="docs" depends="init" description="generate documentation">
    <mkdir dir="${javadoc.dir}" />
    <javadoc sourcepath="${src.dir}"
             destdir="${javadoc.dir}"
             packagenames="${javadoc.packages}"
             use="true"
             windowtitle="mywebwork2"
             private="true"/>
  </target>

  <target name="beautify"
          depends="init"
          description="apply jacobe beautifier to the java sources">
    <apply executable="jacobe">
      <arg value="-nobackup"/>
      <arg value="-overwrite"/>
      <srcfile/>
      <fileset dir="." includes = "**/*.java"/>
    </apply>
  </target>

  <target name= "test" depends="compile" description="run all test cases">
    <junit haltonfailure="yes" fork="yes">
      <classpath refid="project.classpath"/>
      <formatter type="plain" usefile="false"/>
      <batchtest>
        <fileset dir="${test.src.dir}">
          <include name="**/test/*Test.java" />
        </fileset>
      </batchtest>
    </junit>
  </target>

  <target name="pretty-test"
          depends="compile"
          description="HTML output of test cases">
    <mkdir dir="${test.reports.dir}"/>
    <junit haltonfailure="no" fork="yes">
      <classpath refid="project.classpath"/>
      <formatter type="xml" usefile="true"/>

      <batchtest todir="${test.reports.dir}">
        <fileset dir="${src.dir}">
          <include name="**/test/*Test.java" />
        </fileset>
      </batchtest>

    </junit>

    <junitreport todir="${test.reports.dir}">
      <fileset dir="${test.reports.dir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${test.reports.dir}"/>
    </junitreport>
  </target>

  <target name="compile" depends="init" description="compile java">
    <mkdir dir="${build.classes.dir}" />
    <javac srcdir="${src.dir}"
           destdir="${build.classes.dir}"
           classpathref="project.classpath"
           debug="${build.debug}"
           deprecation="on"/>
  </target>

  <target name="init">
    <tstamp />
    <mkdir dir="${lib.dir}"/>
  </target>

</project>
