<?xml version="1.0"?>
<!-- ====================================================================== -->
<!--  Author: Matt Tiedeman                                                 -->
<!--    Info: Visit www.tiedeman.com/ant for more information.              -->
<!-- ====================================================================== -->
<!-- This ANT build file will perform various processes required during the -->
<!-- development phase of a web application.  Currently, the build file     -->
<!-- attempts to determine if an installation of TOMCAT is installed.  If   -->
<!-- so, the ANT installation process will occur within the TOMCAT          -->
<!-- directory tree.  If a TOMCAT installation cannot be located, the       -->
<!-- installation will occur within the 'webPub' directory tree.            -->
<!-- ====================================================================== -->
<project name="Web Application" default="compile" basedir=".">

  <!-- ==================================================================== -->
  <!-- Initializes the basic properties we need to get going.               -->
  <!-- ==================================================================== -->
  <target name="init">
    <!-- ==================================== -->
    <!-- Project paths and file name settings -->
    <!-- ==================================== -->
    <!-- Read in the properties for the build process -->
    <property file="build.properties" />
    <!-- The file seperator character. -->
    <property name="FS" value="${file.separator}" />
    <!-- The path seperator character. -->
    <property name="PS" value="${path.separator}" />
    <!-- Crudely determines if we are using a local copy of Tomcat -->
    <available file="${tomcat}${tomcatServletJar}" property="tomcatInstall" />
  </target>

  <!-- ==================================================================== -->
  <!-- Setup so that install places files within the iPlanet directories.  -->
  <!-- ==================================================================== -->
  <target name="setIPlanetInstall" depends="init" unless="tomcatInstall">
    <echo message="    Unable to locate TOMCAT, assuming iPlanet install." />
    <property name="dest.projectDir" value="${webPub}${webApp}" />
    <property name="classpath.base"
              value=".${PS}${iplanetServletJar}${PS}${partialClasspath}" />
  </target>

  <!-- ==================================================================== -->
  <!-- Setup so that install places files within the TOMCAT directory tree. -->
  <!-- ==================================================================== -->
  <target name="setTomcatInstall" depends="init" if="tomcatInstall">
    <echo message="    Located TOMCAT, installing under TOMCAT." />
    <property name="dest.projectDir"
              value="${tomcat}${FS}webapps${FS}${webApp}" />
    <property name="classpath.base"
              value=".${PS}${tomcat}${tomcatServletJar}${PS}${partialClasspath}" />
  </target>

  <!-- ==================================================================== -->
  <!-- Source and Build file location determination.                        -->
  <!-- ==================================================================== -->
  <target name="setSources" depends="setIPlanetInstall,setTomcatInstall">
    <property name="src.webFiles" value="${basedir}${FS}src${FS}${webApp}" />
    <property name="src.classes" value="${basedir}${FS}src${FS}classes" />
    <property name="src.tlds" value="${basedir}${FS}src${FS}tlds" />
    <property name="src.tests" value="${basedir}${FS}src${FS}test" />
    <property name="build.dir" value="${basedir}${FS}build" />
    <property name="build.webFiles" value="${build.dir}${FS}${webApp}" />
    <property name="build.webinf" value="${build.webFiles}${FS}WEB-INF" />
    <property name="build.classes" value="${build.webinf}${FS}classes" />
    <property name="build.jars" value="${build.webinf}${FS}lib" />
    <property name="build.tlds" value="${build.webinf}${FS}tlds" />
    <property name="build.tests" value="${build.dir}${FS}testcases" />
    <property name="build.docs" value="${build.dir}${FS}docs" />
    <property name="dest.webinf" value="${dest.projectDir}${FS}WEB-INF${FS}" />
    <property name="dest.classes" value="${dest.webinf}classes" />
    <property name="dest.lib" value="${dest.webinf}lib" />
    <property name="dest.tlds" value="${dest.webinf}tlds" />
  </target>

  <!-- ==================================================================== -->
  <!-- Compiles the source code.                                            -->
  <!-- ==================================================================== -->
  <target name="compile" depends="setSources"
          description="- (DEFAULT) Compiles all of the class and jar files.">
    <property name="classpath.compile"
              value="${classpath.base}${PS}${src.classes}" />
    <property name="flag.debug" value="off" />
    <echo message="" />
    <echo message="    Source Directories" />
    <echo message="    ------------------" />
    <echo message="    jsp/html/js/images - ${src.webFiles}" />
    <echo message="    class files        - ${src.classes}" />
    <echo message="    packages           - ${src.classes}${FS}com" />
    <echo message="    tag library desc.  - ${src.tlds}" />
    <echo message="    unit tests         - ${src.tests}" />
    <echo message="" />
    <echo message="    Build Directories" />
    <echo message="    -----------------" />
    <echo message="    jsp/html/js/images - ${build.webFiles}" />
    <echo message="    class files        - ${build.classes}" />
    <echo message="    jar files          - ${build.jars}" />
    <echo message="    tag library desc.  - ${build.tlds}" />
    <echo message="    unit tests         - ${build.tests}" />
    <echo message="    documentation      - ${build.docs}" />
    <echo message="" />
    <echo message="    Compile Flags" />
    <echo message="    -------------" />
    <echo message="    Debug flag is '${flag.debug}'" />
    <echo message="    Optimize flag is '${flag.optimize}'" />
    <echo message="    Deprecation warning flag is '${flag.deprecation}'" />
    <echo message="" />
    <echo message="    CLASSPATH when compiling the Web Application" />
    <echo message="    --------------------------------------------" />
    <echo message="    ${classpath.compile}" />
    <echo message="" />
    <!-- Creates the proper build directories -->
    <mkdir dir="${build.webFiles}" />
    <mkdir dir="${build.classes}" />
    <mkdir dir="${build.jars}" />
    <mkdir dir="${build.tlds}" />
    <mkdir dir="${build.tests}" />
    <mkdir dir="${build.docs}" />
    <!-- Transfer all files within the webFiles tree to their proper place. -->
    <!-- The excluded files are source files for the images and other       -->
    <!-- similar objects (nso - NetStudio, psp - Paint Shop Pro, etc.).	    -->
    <copy todir="${build.webFiles}">
      <fileset dir="${src.webFiles}">
        <exclude name="**${FS}*.nso" />
        <exclude name="**${FS}*.psp" />
        <exclude name="**${FS}*.mic" />
        <exclude name="**${FS}*.psd" />
        <exclude name="**${FS}*.pdd" />
        <exclude name="**${FS}*.ai" />
        <exclude name="**${FS}*.eps" />
      </fileset>
    </copy>
    <!-- Transfer the web.xml file to its proper location -->
    <copy tofile="${build.webinf}${FS}web.xml"
          file="${basedir}${FS}src${FS}web.xml" />
    <!-- Transfer the tag library descriptors to their proper location -->
    <copy todir="${build.tlds}">
      <fileset dir="${src.tlds}" />
    </copy>
    <!-- Compiles the 'src' .java files into 'build' .class files -->
    <javac srcdir="${src.classes}"
           destdir="${build.classes}"
	   classpath="${classpath.compile}"
	   debug="${flag.debug}"
	   optimize="${flag.optimize}"
	   deprecation="${flag.deprecation}" />
    <!-- Calls the JAR file creation handler -->
    <antcall target="jarCreation" />
    <!-- Calls the Unit Testing target -->
    <antcall target="unitTests" />
  </target>

  <!-- ==================================================================== -->
  <!-- Creates a jar file containing all of the files in the com directory  -->
  <!-- tree.  If the 'jarFile' property does not exist, the target will be  -->
  <!-- skipped.                                                             -->
  <!-- ==================================================================== -->
  <target name="jarCreation" if="jarFile">
    <echo message="" />
    <!-- Adds the class files in the 'com' directory tree to a jar file -->
    <jar jarfile="${build.jars}${FS}${jarFile}.jar"
         basedir="${build.classes}"
         includes="com${FS}**" />
  </target>

  <!-- ==================================================================== -->
  <!-- Runs all of the unit tests for this application.  If the 'jUnitJar'  -->
  <!-- property does not exist, the target will be skipped.                 -->
  <!-- ==================================================================== -->
  <target name="unitTests" if="jUnitJar">
    <property name="classpath.unitTests.base"
              value="${classpath.base}${PS}${jUnitJar}${PS}${build.classes}${PS}${build.jars}${FS}${jarFile}.jar" />
    <property name="classpath.unitTests.compile"
              value="${classpath.unitTests.base}${PS}${src.classes}" />
    <property name="classpath.unitTests.run"
              value="${classpath.unitTests.base}${PS}${build.tests}" />
    <echo message="" />
    <echo message="    CLASSPATH when compiling the Unit Tests" />
    <echo message="    ---------------------------------------" />
    <echo message="    ${classpath.unitTests.compile}" />
    <echo message="" />
    <!-- Compiles the unit tests -->
    <javac srcdir="${src.tests}"
           destdir="${build.tests}"
           classpath="${classpath.unitTests.compile}"
	   debug="${flag.debug}"
	   optimize="${flag.optimize}"
	   deprecation="${flag.deprecation}">
    </javac>
    <echo message="" />
    <echo message="    CLASSPATH when running the Unit Tests" />
    <echo message="    -------------------------------------" />
    <echo message="    ${classpath.unitTests.run}" />
    <echo message="" />
    <!-- Run the unit tests -->
    <java classpath="${classpath.unitTests.run}"
          fork="yes"
          classname="junit.textui.TestRunner"
	  taskname="junit"
	  failonerror="true">
      <arg value="test.com.firsthealth.AllJUnitTests" />
    </java>
  </target>

  <!-- ==================================================================== -->
  <!-- Sets the debug flag and then calls the install target                -->
  <!-- ==================================================================== -->
  <target name="debug"
          description="- Sets the debug flags and then calls install target.">
    <property name="flag.debug" value="on" />
    <antcall target="install" />
  </target>

  <!-- ==================================================================== -->
  <!-- Calls the compile target and then copies the generated class files,  -->
  <!-- jar files, jsp files and images into their proper locations.         -->
  <!-- ==================================================================== -->
  <target name="install" depends="compile"
          description="- Compiles and then installs all of the source files.">
    <echo message="" />
    <echo message="    Destination Files" />
    <echo message="    -----------------" />
    <echo message="    jsp/html/js/images - ${dest.projectDir}" />
    <echo message="    class files        - ${dest.classes}" />
    <echo message="    jar files          - ${dest.lib}" />
    <echo message="    tag library desc.  - ${dest.tlds}" />
    <echo message="" />
    <mkdir dir="${dest.projectDir}" />
    <mkdir dir="${dest.classes}" />
    <mkdir dir="${dest.lib}" />
    <mkdir dir="${dest.tlds}" />
    <!-- Change permissions on the build files so that they will have the   -->
    <!-- proper permissions on them after moving them around.               -->
    <chmod perm="ug=rw,o=r">
      <fileset dir="${build.dir}">
        <include name="**${FS}*.class" />
        <include name="**${FS}*.jar" />
        <include name="**${FS}*.html" />
        <include name="**${FS}*.jsp" />
        <include name="**${FS}*.gif" />
        <include name="**${FS}*.jpg" />
        <include name="**${FS}*.pdf" />
      </fileset>
    </chmod>
    <!-- Transfers the web files (HTML,JSP,JS,GIF,JPG,PDF,etc.) that are    -->
    <!-- within the 'webFiles' tree.                                        -->
    <copy todir="${dest.projectDir}">
      <fileset dir="${build.webFiles}">
        <exclude name="META-INF" />
        <exclude name="WEB-INF" />
      </fileset>
    </copy>
    <!-- Transfer the web.xml file  -->
    <copy tofile="${dest.webinf}${FS}web.xml"
          file="${build.webinf}${FS}web.xml" />
    <!-- Transfer the class files -->
    <copy todir="${dest.classes}">
      <fileset dir="${build.classes}" />
    </copy>
    <!-- Transfer the jar files -->
    <copy todir="${dest.lib}">
      <fileset dir="${build.jars}" />
    </copy>
    <!-- Transfer the tag library descriptor files -->
    <copy todir="${dest.tlds}">
      <fileset dir="${build.tlds}" />
    </copy>
  </target>

  <!-- ==================================================================== -->
  <!-- Removes the build directory and all of its contents.                 -->
  <!-- ==================================================================== -->
  <target name="clean" depends="setSources"
          description="- Removes the build directory and all of its contents.">
    <delete dir="${build.dir}" />
  </target>

  <!-- ==================================================================== -->
  <!-- Produces the JavaDoc documenation for the current application.       -->
  <!-- ==================================================================== -->
  <target name="javadoc" depends="compile"
          description="- Uses JavaDoc to create the web app's documentation.">
    <property name="classpath.javadoc"
              value="${classpath.base}${PS}${src.classes}" />
    <echo message="" />
    <echo message="    CLASSPATH when generating the Documentation" />
    <echo message="    -------------------------------------------" />
    <echo message="    ${classpath.javadoc}" />
    <echo message="" />
    <javadoc packagenames="${src.classes}${FS}*.java,com.*"
             sourcepath="${src.classes}"
             destdir="${build.docs}"
	     classpath="${classpath.javadoc}"
             author="true"
             version="true"
             windowtitle="${appDesc} API"
             doctitle="${appDesc}"
             bottom="Copyright 2001 First Health. All Rights Reserved.">
    </javadoc>
  </target>

  <!-- ==================================================================== -->
  <!-- Calls the compile target.  After compile completes, the build        -->
  <!-- directories are used to produce a WAR file.                          -->
  <!-- ==================================================================== -->
  <target name="warfile" depends="compile"
          description="- Compiles source code and then creates a WAR file.">
    <war warfile="${build.dir}${FS}${webApp}.war"
         webxml="${build.webinf}${FS}web.xml">
      <fileset dir="${build.webFiles}">
        <exclude name="WEB-INF${FS}**" />
      </fileset>
      <webinf dir="${build.webinf}">
        <exclude name="classes${FS}**" />
        <exclude name="lib${FS}**" />
        <exclude name="web.xml" />
      </webinf>
      <classes dir="${build.classes}">
        <exclude name="com${FS}**" />
      </classes>
      <lib dir="${build.jars}" />
    </war>
  </target>
</project>

