<?xml version="1.0"?>

<project name="WebWork" default="main" basedir=".">

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

  <target name="init">
    <property file="build.properties"/>
    <property name="name" value="webwork"/>
    <property name="version" value="1.4"/>
    <property name="packages" value="webwork.*"/>
    <property name="lib.dir" value="${basedir}/lib"/>
    <property name="src.dir" value="${basedir}/src"/>
    <property name="build.dir" value="${basedir}/build"/>
    <property name="dist.dir" value="${basedir}/dist"/>
    <available property="javacc.optional.available" classname="org.apache.tools.ant.taskdefs.optional.javacc.JavaCC"/>
    <available property="junit.available" classname="junit.framework.TestCase"/>
    <available property="clover.available" classname="org.apache.tools.ant.taskdefs.CloverCompilerAdapter"/>
  </target>

  <target name="check-javacc" depends="init" unless="javacc.optional.available">
    <fail message="Ant optional.jar is not available, so cannot run JavaCC task. Please download ant 1.5.3 or later."/>
  </target>

  <target name="junit-check" depends="init" unless="junit.available">
    <fail message="Cannot run test cases. Please copy lib/build/junit-3.8.1.jar to ${ant.home}/lib"/>
  </target>

  <target name="clover-check" depends="init" unless="clover.available">
    <fail message="Cannot run coverage tests. Please copy lib/build/clover-1.2.3.jar to ${ant.home}/lib"/>
  </target>

  <target name="server-check" depends="init" unless="test.server">
    <fail message="Cannot run web tests. Please define test.server and test.context in your build.properties file"/>
  </target>

  <target name="init-clover" depends="init">
    <taskdef resource="clovertasks"/>
    <typedef resource="clovertypes"/>
    <mkdir dir="clover-db"/>
    <clover-setup initstring="clover-db/coverage.db">
    <fileset dir="${src.dir}/main">
      <exclude name="webwork/examples/**" />
    </fileset>
    </clover-setup>
  </target>


  <target name="jasperreports" depends="compile" description="Compile jasper reports">

    <path id="jasperreports.class.path">
      <pathelement location="${build.dir}/result/web/WEB-INF/classes"/>
      <fileset dir="${basedir}/lib">
        <include name="**/*.jar"/>
      </fileset>
    </path>

    <java classname="webwork.view.jasperreports.CompileReport" fork="true">
      <!-- still having problems using crimson
      <sysproperty key="org.xml.sax.driver" value="org.apache.crimson.parser.XMLReaderImpl"/>
      -->
      <sysproperty key="java.awt.headless" value="true" />
      <sysproperty key="org.xml.sax.driver" value="org.apache.xerces.parsers.SAXParser"/>
      <arg value="${src.dir}/resources/web/tests/jasperreports/orderReport.xml"/>
      <arg value="${src.dir}/resources/web/tests/jasperreports/orderList.xml"/>
      <classpath refid="jasperreports.class.path"/>
    </java>
    <move todir="${build.dir}/result/web">
      <fileset dir="${src.dir}/resources/web">
        <include name="**/*.jasper" />
      </fileset>
    </move>
  </target>

  <target name="test-web" depends="clover-check,junit-check,precompile,init-clover" description="Build webapp to be deployed for web-based test cases">
    <property name="test.classes" value="${build.dir}/result/test-web/WEB-INF/classes"/>
    <mkdir dir="${test.classes}"/>
    <javac srcdir="${src.dir}/main" destdir="${test.classes}" debug="on" classpathref="core.class.path" compiler="org.apache.tools.ant.taskdefs.CloverCompilerAdapter"/>
    <!-- copy all default resources -->
    <copy todir="${test.classes}">
      <fileset dir="${src.dir}/main">
        <include name="**/*.properties"/>
        <include name="**/*.vm"/>
        <include name="**/*.xml"/>
      </fileset>
      <fileset dir="${src.dir}/test">
        <include name="**/*.xml"/>
        <include name="**/*.txt"/>
      </fileset>
    </copy>

    <copy todir="${build.dir}/result/test-web">
      <fileset dir="${src.dir}/resources/web">
        <include name="tests/**"/>
        <include name="template/**"/>
        <include name="**/*.properties"/>
        <include name="**/*.vm"/>
        <include name="**/*.xml"/>
        <include name="**/*.tld"/>
      </fileset>
    </copy>

    <mkdir dir="${build.dir}/result/test-web/WEB-INF/lib"/>
    <jar jarfile="${build.dir}/result/test-web/WEB-INF/lib/${name}.jar">
      <fileset dir="${test.classes}">
        <exclude name="**/examples/**"/>
        <exclude name="*.properties"/>
        <exclude name="*.xml"/>
        <exclude name="**/*.html"/>
      </fileset>
    </jar>
    <copy todir="${build.dir}/result/test-web/WEB-INF/lib" flatten="true">
      <fileset dir="${lib.dir}">
        <include name="**/clover*.jar"/>
        <include name="**/core/**"/>
        <include name="**/jdom*.jar"/>
      </fileset>
    </copy>

    <javac srcdir="${src.dir}/test" destdir="${test.classes}" classpathref="core.class.path" debug="on"/>

  </target>

  <target name="test" depends="clover-check,junit-check,init-clover,server-check" description="Run standalone and web-based test cases">

    <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"/>
    <property name="test.classes" value="${build.dir}/result/test-web/WEB-INF/classes"/>

    <junit printsummary="withOutAndErr" haltonfailure="no" haltonerror="no" showoutput="true" fork="true">
      <!-- pass in build.properties settings, so we can pick up the test url from there -->
      <sysproperty key="test.server" value="${test.server}" />
      <sysproperty key="test.context" value="${test.context}"/>
      <classpath>
        <pathelement location="${test.classes}"/>
        <path refid="core.class.path"/>
      </classpath>

      <formatter type="plain"/>

      <batchtest todir="${build.dir}/result/test-web">
        <fileset dir="${src.dir}/test">
          <include name="**/*TestCase.java"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

  <target name="clover.report" depends="clover-check,init-clover" description="Generate clover coverage reports, test target must be run first">
    <mkdir dir="${build.dir}/clover"/>
    <clover-report>
      <current title="WebWork Coverage Report" outfile="${build.dir}/result/clover">
        <format type="html"/>
      </current>
    </clover-report>
  </target>

  <target name="precompile" depends="check-javacc" description="Generate expression language parser">
    <javacc target="${src.dir}/main/webwork/expr/Parser.jj" javacchome="${lib.dir}/build"/>
  </target>

  <target name="compile" depends="precompile" description="Compile all core classes">
    <mkdir dir="${build.dir}/result/web/WEB-INF/classes"/>
    <javac srcdir="${src.dir}/main" destdir="${build.dir}/result/web/WEB-INF/classes" debug="on" deprecation="off" optimize="off">
      <classpath refid="core.class.path"/>
    </javac>
  </target>

  <target name="webapp" depends="compile,jasperreports" description="Create example webapp">

    <!-- JAR files -->
    <copy todir="${build.dir}/result/web">
      <fileset dir="${src.dir}/resources/web"/>
    </copy>

    <copy todir="${build.dir}/result/web/WEB-INF/classes">
      <fileset dir="${src.dir}/main">
        <exclude name="**/*.java"/>
      </fileset>
    </copy>

    <mkdir dir="${build.dir}/result/web/WEB-INF/lib"/>

    <jar jarfile="${build.dir}/result/web/WEB-INF/lib/${name}.jar">
      <fileset dir="${build.dir}/result/web/WEB-INF/classes">
        <exclude name="**/examples/**" />
        <exclude name="*.properties"/>
        <exclude name="*.xml"/>
        <exclude name="**/*.html"/>
      </fileset>
    </jar>

    <jar jarfile="${build.dir}/result/web/WEB-INF/lib/${name}-examples.jar">
      <fileset dir="${build.dir}/result/web/WEB-INF/classes">
        <include name="**/examples/**"/>
        <include name="*.properties"/>
        <include name="*.xml"/>
      </fileset>
    </jar>

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

    <jar jarfile="${build.dir}/lib/${name}client.jar">
      <fileset dir="${build.dir}/result/web/WEB-INF/classes">
        <include name="webwork/action/client/**"/>
        <include name="webwork/action/*.*"/>
        <include name="webwork/util/editor/PropertyEditorException.class" />
      </fileset>
    </jar>

    <!-- all this is for the hello world applet example -->
    <copy todir="${build.dir}/result/web/examples" file="${build.dir}/lib/${name}client.jar" />
    <copy todir="${build.dir}/result/web/examples" file="${lib.dir}/core/commons-logging.jar" />
    <copy todir="${build.dir}/result/web/examples/" file="${build.dir}/lib/${name}client.jar" />
    <jar jarfile="${build.dir}/result/web/examples/helloworld.jar"
      basedir="${build.dir}/result/web/WEB-INF/classes"
      includes="**/examples/helloworld/**"/>

    <copy todir="${build.dir}/result/web/WEB-INF/lib" includeEmptyDirs="no" flatten="yes">
      <fileset dir="${lib.dir}">
        <exclude name="build/**" />
      </fileset>
    </copy>

    <copy todir="${build.dir}/lib">
      <fileset dir="${build.dir}/result/web/WEB-INF/lib" includes="webwork*.jar"/>
    </copy>

  </target>

  <target name="skeleton" depends="webapp" description="Build skeleton webapp">
    <mkdir dir="${build.dir}/skeleton/WEB-INF/lib" />
    <copy todir="${build.dir}/skeleton">
      <fileset dir="${src.dir}/resources/skeleton" />
      <fileset dir="${src.dir}/resources/web">
        <include name="template/**" />
      </fileset>
    </copy>
    <copy todir="${build.dir}/skeleton/WEB-INF/lib">
      <fileset file="${lib.dir}/core/commons-logging.jar"/>
      <fileset file="${build.dir}/result/web/WEB-INF/lib/${name}.jar"/>
    </copy>
  </target>

  <target name="war" depends="webapp" description="Build example war webapp">
    <mkdir dir="${build.dir}/deploy" />
    <war jarfile="${build.dir}/deploy/${name}.war" webxml="${build.dir}/result/web/WEB-INF/web.xml">
      <fileset dir="${build.dir}/result/web">
        <exclude name="WEB-INF/classes/webwork/**" />
        <exclude name="WEB-INF/web.xml" />
      </fileset>
    </war>
  </target>

  <target name="ear" depends="war" description="Build example ear application">
    <ear appxml="${src.dir}/resources/ear/META-INF/application.xml" earfile="${build.dir}/deploy/${name}.ear">
      <fileset dir="${build.dir}/deploy/">
        <include name="*.war" />
      </fileset>
    </ear>
  </target>

  <target name="main" depends="webapp" description="Compile and build webapp and webwork jars">

    <mkdir dir="${dist.dir}/lib"/>

    <copy todir="${dist.dir}/lib">
      <fileset dir="${build.dir}/lib"/>
    </copy>
    <copy todir="${dist.dir}/lib">
      <fileset dir="${build.dir}/result/web/WEB-INF/lib/">
        <include name="**/webwork*.jar"/>
      </fileset>
    </copy>
  </target>

  <target name="docs" depends="precompile,javadocs" description="Generate all documentation">
    <copy tofile="${build.dir}/result/web/docs/actions.xml.txt" file="${src.dir}/resources/web/WEB-INF/classes/actions.xml"/>
    <copy todir="${build.dir}/result/web/docs/" file="${src.dir}/etc/actions.dtd"/>
    <copy todir="${build.dir}/result/web/docs/" file="${src.dir}/main/webwork/default.properties"/>

    <copy todir="${build.dir}/result/web/docs/">
      <fileset dir="${src.dir}/docs/manual" includes="**/*"/>
      <fileset dir="${src.dir}/docs" includes="cookbook/*"/>
    </copy>
  </target>

  <target name="javadocs" depends="init" description="Build javadocs">
    <javadoc packagenames="${packages}" sourcepath="${src.dir}/main" destdir="${build.dir}/result/web/docs/api"
      author="true" version="true" windowtitle="${ant.project.name} API" doctitle="${ant.project.name}"
      stylesheetfile="${src.dir}/docs/api/stylesheet.css" classpathref="core.class.path"
      bottom="Copyright &#169; 2001-2003 ${ant.project.name} All Rights Reserved.">
      <group title="WebWork Action API" packages="webwork.action*"/>
      <group title="WebWork Configuration" packages="webwork.config"/>
      <group title="WebWork Dispatchers" packages="webwork.dispatcher"/>
      <group title="WebWork JSP Taglibs" packages="webwork.view.taglib*"/>
      <group title="WebWork Velocity Support" packages="webwork.view.velocity*"/>
      <group title="WebWork XSLT Support" packages="webwork.view.xslt*"/>
      <group title="WebWork Examples" packages="webwork.examples*"/>
    </javadoc>
  </target>

  <target name="dist" depends="docs,main,skeleton,ear" description="Build distribution directory">
    <copy todir="${dist.dir}/etc">
      <fileset dir="${src.dir}/etc"/>
    </copy>
    <zip basedir="${build.dir}/skeleton/" file="${dist.dir}/etc/skeleton-project.zip" />
    <copy todir="${dist.dir}/src">
      <fileset dir="${src.dir}/main"/>
    </copy>
    <copy todir="${dist.dir}" file="${build.dir}/deploy/${name}.war" />
    <copy todir="${dist.dir}/docs">
      <fileset dir="${build.dir}/result/web/docs"/>
    </copy>
    <copy todir="${dist.dir}">
      <fileset dir="${basedir}">
        <include name="*.txt" />
      </fileset>
    </copy>
  </target>


  <target name="dist-zip" depends="dist" description="Build distribution zipfile">
    <copy todir="${basedir}/${name}-${version}">
      <fileset dir="${dist.dir}"/>
    </copy>
    <zip zipfile="${dist.dir}/${name}-${version}.zip" basedir="${basedir}" includes="${name}-${version}/**"/>
    <delete dir="${basedir}/${name}-${version}"/>
  </target>

  <target name="dist-bz2" depends="dist" description="Build distribution bz2 file">
    <copy todir="${basedir}/${name}-${version}">
      <fileset dir="${dist.dir}"/>
    </copy>
    <tar tarfile="${dist.dir}/${name}-${version}.tar.bz2" basedir="${basedir}" includes="${name}-${version}/**" compression="bzip2" />
    <delete dir="${basedir}/${name}-${version}"/>
  </target>

  <target name="clean" depends="init" description="Remove all build artifacts">
    <delete dir="${build.dir}"/>
    <delete dir="${dist.dir}"/>
    <delete dir="clover-db"/>
    <delete>
      <fileset dir="${src.dir}/main/webwork/expr" includes="**/*.java"/>
    </delete>
  </target>

</project>


