<?xml version="1.0"?>
<!DOCTYPE project [
    <!ENTITY common SYSTEM "file:../build-common.xml">
]>

<!--
  =============================================================================
    Build file for the Cactus framework.

    $Id: build.xml 238998 2004-05-23 14:11:52Z vmassol $
  =============================================================================
-->
<project name="Cactus Framework" default="dist" basedir="..">

  <description>
 Cactus Framework
 ---------------------------------------------------------
 Contains the core classes of the Cactus in-container 
 testing framework.
  </description>

  <!-- Base directory for all file related operations -->
  <property name="base.dir" location="framework"/>

  <!-- Indicate that this subproject is dependant on the version of the J2EE
       API available -->
  <property name="j2ee.dependant" value="true"/>

  <!-- Include properties and targets common to the different subprojects -->
  &common;

  <!-- Global project properties -->
  <property name="project.name.text" value="Cactus Framework"/>
  <property name="project.name.file" value="cactus-framework"/>

  <!--
     ========================================================================
       Initialize source, target and dist properties
     ========================================================================
  -->
  <target name="properties">

    <!-- Set the properties related to the source tree -->
    <property name="src.dir" location="${base.dir}/src"/>
    <property name="src.java.dir" location="${src.dir}/java"/>
    <property name="src.test.dir" location="${src.dir}/test"/>
    <property name="src.aspect.dir" location="${src.dir}/aspect"/>
    <property name="build.dir" location="${base.dir}/."/>
    <property name="conf.dir" location="${base.dir}/conf"/>
    <property name="web.dir" location="${base.dir}/web"/>

    <!-- Set the properties related to the target area -->
    <property name="target.src.dir" location="${target.dir}/src"/>
    <property name="target.classes.dir" location="${target.dir}/classes"/>
    <property name="target.classes.java.dir"
        location="${target.classes.dir}/java"/>
    <property name="target.classes.clover.dir"
        location="${target.classes.dir}/clover"/>
    <property name="target.classes.test.dir"
        location="${target.classes.dir}/test"/>
    <property name="target.doc.dir" location="${target.dir}/doc"/>
    <property name="target.doc.api.dir" location="${target.doc.dir}/api"/>

    <!-- Ensure that there is always a default for the container port -->
    <property name="cactus.port" value="8080"/>

  </target>

  <!--
     ========================================================================
       Initialize Ant tasks
     ========================================================================
  -->
  <target name="init.aspectj">

    <!-- Define the AspectJ tasks -->
    <taskdef
        resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
      <classpath>
        <pathelement location="${aspectj-tools.jar}"/>
      </classpath>
    </taskdef>

  </target>

  <!--
     ========================================================================
       Initialize the build. Must be called by all targets
     ========================================================================
  -->
  <target name="init" 
      depends="init.common, properties, init.aspectj, init.clover">

    <echo>Dependencies:</echo>
    <echo>  aspectjrt.jar = [${aspectjrt.jar}]</echo>
    <echo>  commons.httpclient.jar = [${commons.httpclient.jar}]</echo>
    <echo>  commons.logging.jar = [${commons.logging.jar}]</echo>
    <echo>  httpunit.jar = [${httpunit.jar}]</echo>
    <echo>  j2ee.jar = [${j2ee.jar}]</echo>
    <echo>  servlet.jar = [${servlet.jar}]</echo>
    <echo>  jsp.jar = [${jsp.jar}]</echo>
    <echo>  junit.jar = [${junit.jar}]</echo>
    <echo>  mockobjects.jar = [${mockobjects.jar}]</echo>
    <echo>  jetty.jar = [${jetty.jar}]</echo>
    <echo>  log4j.jar (optional) = [${log4j.jar}]</echo>
    <echo>  xmlapis.jar (optional) = [${xmlapis.jar}]</echo>
    <echo/>
    <echo>Options:</echo>
    <echo>  clover.enable = ${clover.enable}</echo>
    <echo>  clover.jar = [${clover.jar}]</echo>
    <echo>  cactus.port = [${cactus.port}]</echo>
  
    <path id="project.classpath">
      <pathelement location="${aspectjrt.jar}"/>
      <pathelement location="${commons.logging.jar}"/>
      <pathelement location="${commons.httpclient.jar}"/>
      <pathelement location="${httpunit.jar}"/>
      <pathelement location="${j2ee.jar}"/>
      <pathelement location="${servlet.jar}"/>
      <pathelement location="${jsp.jar}"/>
      <pathelement location="${junit.jar}"/>
      <pathelement location="${xmlapis.jar}"/>
    </path>

    <condition property="properties.ok">
      <and>
        <available file="${aspectjrt.jar}"/>
        <available file="${commons.httpclient.jar}"/>
        <available file="${commons.logging.jar}"/>
        <available file="${httpunit.jar}"/>
        <available file="${j2ee.jar}"/>
        <available file="${servlet.jar}"/>
        <available file="${jsp.jar}"/>
        <available file="${junit.jar}"/>
        <available file="${mockobjects.jar}"/>
        <available file="${jetty.jar}"/>
        <!-- Check for a TraX class instead of the xmlapis.jar file so that
             the build works on JDK 1.4 without needing to define the
             xmlapis.jar property -->
        <available classname="javax.xml.transform.Transformer"
            classpathref="project.classpath"/>
        <!-- Check that clover.jar points to a valid file if clover is 
             enabled -->
        <or>
          <and>        
            <isset property="clover.enable"/>
            <available file="${clover.jar}"/>
          </and>
          <not>
            <isset property="clover.enable"/>
          </not>
        </or>            
      </and>
    </condition>

    <fail unless="properties.ok">One or more required dependencies could not
be resolved. Please check you build.properties file, and run Ant with the 
-verbose option for more details</fail>

  </target>

  <!--
     ========================================================================
       Compiles the sources
     ========================================================================
  -->
  <target name="compile.prepare"
      depends="init">

    <!-- Copy the shared source files to resolve the project version in the
         Version.java source file -->
    <copy todir="${target.src.dir}" filtering="on">
      <fileset dir="${src.java.dir}/share-12-13-14"/>
      <fileset dir="${src.java.dir}/share-13-14">
        <or>
          <selector if="j2ee13.available"/>
          <selector if="j2ee14.available"/>
        </or>
      </fileset>              
    </copy>

  </target>

  <target name="compile.clover"
      depends="compile.prepare"
      if="clover.enable">

    <clover-setup initstring="${clover.initstring}" enabled="yes">
      <files>
        <exclude name="**/*Aspect.*"/>
        <exclude name="**/Test*.*"/>
        <exclude name="**/Mock*.*"/>
        <exclude name="**/AssertUtils.*"/>
        <exclude name="**/ClientCookie.*"/>
      </files>
    </clover-setup>

    <mkdir dir="${target.classes.clover.dir}"/>

    <javac destdir="${target.classes.clover.dir}"
        debug="${debug}" deprecation="${deprecation}"
        optimize="${optimize}">
      <src>
        <pathelement location="${target.src.dir}"/>
        <dirset dir="${src.java.dir}">
          <or>
            <and>
              <filename name="j2ee-12"/>
              <selector if="j2ee12.available"/>
            </and>
            <and>
              <filename name="j2ee-13"/>
              <selector if="j2ee13.available"/>
            </and>
            <and>
              <filename name="j2ee-14"/>
              <selector if="j2ee14.available"/>
            </and>
          </or>
        </dirset>
        <pathelement location="${src.aspect.dir}"/>
      </src>
      <exclude name="**/package.html"/>
      <exclude name="**/overview.html"/>
      <classpath refid="project.classpath"/>
    </javac>

    <copy todir="${target.classes.clover.dir}/org/apache/cactus/server"
        file="${web.dir}/jspRedirector.jsp"/>

    <!-- Disable Clover for subsequent compilations -->
    <clover-setup initstring="${clover.initstring}" enabled="no"/>

  </target>

  <target name="compile.test" depends="compile.main">

    <mkdir dir="${target.classes.test.dir}"/>

    <javac destdir="${target.classes.test.dir}"
        debug="${debug}" deprecation="${deprecation}"
        optimize="${optimize}">
      <src>
        <pathelement location="${src.test.dir}/share-12-13-14"/>
        <dirset dir="${src.test.dir}">
          <or>
            <and>
              <filename name="share-13-14"/>
              <or>
                <selector if="j2ee13.available"/>
                <selector if="j2ee14.available"/>
              </or>
            </and>
            <and>
              <filename name="j2ee-12"/>
              <selector if="j2ee12.available"/>
            </and>
            <and>
              <filename name="j2ee-13"/>
              <selector if="j2ee13.available"/>
            </and>
            <and>
              <filename name="j2ee-14"/>
              <selector if="j2ee14.available"/>
            </and>
          </or>
        </dirset>
      </src>
      <classpath>
        <path path="${target.classes.java.dir}"/>
        <path refid="project.classpath"/>
        <pathelement location="${mockobjects.jar}"/>
        <pathelement location="${jetty.jar}"/>
      </classpath>
    </javac>

  </target>

  <target name="compile.main.prepare" depends="compile.prepare">

    <!-- Only recompile if source files have changed since the last time
         the main jar was created -->
    <condition property="compile.main.required">
      <not>
        <uptodate targetfile="${target.dir}/${cactus.jar.name}.jar">
          <srcfiles dir="${target.src.dir}"/>    
          <srcfiles dir="${src.java.dir}/j2ee-12">
            <selector if="j2ee12.available"/>
          </srcfiles>
          <srcfiles dir="${src.java.dir}/j2ee-13">
            <selector if="j2ee13.available"/>
          </srcfiles>
          <srcfiles dir="${src.java.dir}/j2ee-14">
            <selector if="j2ee14.available"/>
          </srcfiles>
          <srcfiles dir="${src.aspect.dir}"/>
        </uptodate>
      </not>
    </condition>

  </target>

  <target name="compile.main" depends="init, compile.main.prepare"
      if="compile.main.required">

    <mkdir dir="${target.classes.java.dir}"/>

    <iajc destdir="${target.classes.java.dir}" verbose="${verbose}"
        debug="${debug}" deprecation="${deprecation}"
        sourceRootCopyFilter="**/CVS/*,**/*.java,**/*.html,**/.keepit,**/*.aj">
      <sourceroots>
        <pathelement location="${target.src.dir}"/>
        <dirset dir="${src.java.dir}">
          <or>
            <and>
              <filename name="j2ee-12"/>
              <selector if="j2ee12.available"/>
            </and>
            <and>
              <filename name="j2ee-13"/>
              <selector if="j2ee13.available"/>
            </and>
            <and>
              <filename name="j2ee-14"/>
              <selector if="j2ee14.available"/>
            </and>
          </or>
        </dirset> 
        <pathelement location="${src.aspect.dir}"/>
      </sourceroots>
      <classpath refid="project.classpath"/>
    </iajc>

    <copy todir="${target.classes.java.dir}/org/apache/cactus/server"
        file="${web.dir}/jspRedirector.jsp"/>

  </target>

  <target name="compile" depends="compile.main,compile.test,compile.clover"
      description="Compile the sources"/>

  <!--
     ========================================================================
       Create the runtime jar file
     ========================================================================
  -->
  <target name="jar.clover" depends="compile.clover" if="clover.enable">

    <jar jarfile="${target.dir}/${cactus.clover.jar.name}.jar">
      <manifest>
        <section name="org/apache/cactus/">
          <attribute name="Specification-Title"
              value="${project.name.text} for ${j2ee.title}"/>
          <attribute name="Specification-Version"
              value="${project.version}"/>
          <attribute name="Specification-Vendor"
              value="Apache Software Foundation"/>
          <attribute name="Implementation-Title"
              value="${project.name.file}"/>
          <attribute name="Implementation-Version"
              value="${project.version} ${TODAY}"/>
          <attribute name="Implementation-Vendor"
              value="Apache Software Foundation"/>
        </section>
      </manifest>
      <metainf dir=".">
        <include name="LICENSE.cactus"/>
        <include name="NOTICE.cactus"/>
      </metainf>
      <fileset dir="${target.classes.clover.dir}"/>
    </jar>

  </target>

  <target name="jar.main"
      depends="compile.main">

    <jar jarfile="${target.dir}/${cactus.jar.name}.jar">

      <manifest>
        <section name="org/apache/cactus/">
          <attribute name="Specification-Title"
              value="${project.name.text} for ${j2ee.title}"/>
          <attribute name="Specification-Version"
              value="${project.version}"/>
          <attribute name="Specification-Vendor"
              value="Apache Software Foundation"/>
          <attribute name="Implementation-Title"
              value="${project.name.file}"/>
          <attribute name="Implementation-Version"
              value="${project.version} ${TODAY}"/>
          <attribute name="Implementation-Vendor"
              value="Apache Software Foundation"/>
        </section>
      </manifest>

      <metainf dir=".">
        <include name="LICENSE.cactus"/>
      </metainf>
      <fileset dir="${target.classes.java.dir}"/>
    </jar>

  </target>

  <target name="jar" depends="jar.main,jar.clover"
      description="Generate the library jar"/>

  <!--
     ========================================================================
       Generate the documentation (javadoc)
     ========================================================================
  -->
  <target name="doc.check" depends="init">

    <uptodate property="doc.uptodate"
        targetfile="${target.doc.api.dir}/index.html">
      <srcfiles dir="${src.java.dir}/j2ee-12" includes="**/*.java, **/*.html">
        <selector if="j2ee12.available"/>
      </srcfiles>
      <srcfiles dir="${src.java.dir}/j2ee-13" includes="**/*.java, **/*.html">
        <selector if="j2ee13.available"/>
      </srcfiles>
      <srcfiles dir="${src.java.dir}/j2ee-14" includes="**/*.java, **/*.html">
        <selector if="j2ee14.available"/>
      </srcfiles>
      <srcfiles dir="${src.java.dir}/share-12-13-14" 
          includes="**/*.java, **/*.html"/>
      <srcfiles dir="${src.java.dir}/share-13-14"
          includes="**/*.java, **/*.html">
        <or>
          <selector if="j2ee13.available"/>
          <selector if="j2ee14.available"/>
        </or>
      </srcfiles>
    </uptodate>

  </target>

  <target name="doc" depends="doc.check" unless="doc.uptodate"
      description="Generates the API documentation">

    <mkdir dir="${target.doc.api.dir}"/>

    <!-- Ideally we should use Ajdoc. However, the generated output is not
         as nice as the javadoc output. In addition, ajdoc does not 
         currently work with the JDK 1.4 (at least until Ajdoc 1.0.6). -->
    <javadoc
        packagenames="org.apache.cactus.*"
        destdir="${target.doc.api.dir}"
        author="true"
        protected="true"
        version="true"
        use="true"
        windowtitle="${project.name.text} ${project.doc.version}"
        doctitle="${project.name.text} ${project.doc.version}"
        overview="${src.java.dir}/share-12-13-14/overview.html"
        bottom="Copyright &amp;copy; ${year} Apache Software Foundation.
                All Rights Reserved.">
      <group title="Public API">
        <package name="org.apache.cactus"/>
        <package name="org.apache.cactus.client.authentication"/>
        <package name="org.apache.cactus.extension.jsp"/>
        <package name="org.apache.cactus.server"/>
        <package name="org.apache.cactus.server.runner"/>
        <package name="org.apache.cactus.util"/>
        <package name="org.apache.cactus.extension.jetty"/>
      </group>
      <group title="Public SPI">
        <package name="org.apache.cactus.spi.client"/>
        <package name="org.apache.cactus.spi.client.connector"/>
        <package name="org.apache.cactus.spi.server"/>
      </group>
      <group title="Internal API">
        <package name="org.apache.cactus.internal"/>
        <package name="org.apache.cactus.internal.configuration"/>
        <package name="org.apache.cactus.internal.client"/>
        <package name="org.apache.cactus.internal.client.connector.http"/>
        <package name="org.apache.cactus.internal.server"/>
        <package name="org.apache.cactus.internal.server.runner"/>
        <package name="org.apache.cactus.internal.util"/>
      </group>
      <sourcepath>
        <pathelement location="${src.java.dir}/share-12-13-14"/>
        <dirset dir="${src.java.dir}">
          <or>
            <and>
              <filename name="share-13-14"/>
              <or>
                <selector if="j2ee13.available"/>
                <selector if="j2ee14.available"/>
              </or>
            </and>
            <and>
              <filename name="j2ee-12"/>
              <selector if="j2ee12.available"/>
            </and>
            <and>
              <filename name="j2ee-13"/>
              <selector if="j2ee13.available"/>
            </and>
            <and>
              <filename name="j2ee-14"/>
              <selector if="j2ee14.available"/>
            </and>
          </or>
        </dirset>
      </sourcepath>
      <classpath refid="project.classpath"/>
    </javadoc>

  </target>

  <!--
     ========================================================================
       Run the full suite of unit tests.
     ========================================================================
  -->
  <target name="test.clover.prepare" depends="compile.test, compile.clover"
      if="clover.enable">

    <path id="test.class.path">
      <pathelement path="${target.classes.clover.dir}"/>
      <pathelement path="${target.classes.test.dir}"/>
      <pathelement location="${clover.jar}"/>
      <pathelement location="${log4j.jar}"/>
      <pathelement location="${mockobjects.jar}"/>
      <pathelement location="${jetty.jar}"/>
      <path refid="project.classpath"/>
    </path>

  </target>

  <target name="test.main.prepare" depends="compile.test" 
      unless="clover.enable">

    <path id="test.class.path">
      <pathelement path="${target.classes.java.dir}"/>
      <pathelement path="${target.classes.test.dir}"/>
      <pathelement location="${log4j.jar}"/>
      <pathelement location="${mockobjects.jar}"/>
      <pathelement location="${jetty.jar}"/>
      <path refid="project.classpath"/>
    </path>

  </target>

  <target name="test.prepare" depends="test.clover.prepare, test.main.prepare">

	<!-- Convert the path to logging.properties to use forward slashes
	     so that it works when loaded as a String in Java (backslashes
	     won't work. -->
	<pathconvert property="target.dir.normalized" dirsep="/">
	  <path>
	    <pathelement location="${target.dir}"/>
	  </path>
	</pathconvert>
	<copy todir="${target.dir}" filtering="on" failonerror="false"
		file="${base.dir}/logging.properties">
	  <filterset>
        <filter token="target.dir" value="${target.dir.normalized}"/>
      </filterset>
	</copy>
    <!-- Make sure a logging.properties file exists, even if empty -->
    <touch file="${target.dir}/logging.properties"/>

  </target>

  <target name="test" depends="test.prepare"
      description="Run the unit tests">

    <junit printsummary="yes" haltonfailure="yes" fork="yes">
      <sysproperty key="cactus.logging.config" 
          value="${target.dir.normalized}/logging.properties"/>
      <sysproperty key="cactus.port" value="${cactus.port}"/>
      <classpath>
        <path refid="test.class.path"/>
      </classpath>
      <formatter type="plain" usefile="false"/>
      <test name="org.apache.cactus.TestAll"/>
    </junit>

  </target>

  <!--
     ========================================================================
       Generate the distributable files
     ========================================================================
  -->
  <target name="dist.clover" depends="jar.clover" if="clover.enable">

    <copy todir="${dist.lib.dir}"
        file="${target.dir}/${cactus.clover.jar.name}.jar"/>

  </target>

  <target name="dist" depends="checkstyle,test,doc,jar.main,dist.clover"
      description="Generate the distributable files">

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

    <copy todir="${dist.lib.dir}"
        file="${target.dir}/${cactus.jar.name}.jar"/>
    <copy todir="${dist.doc.api.dir}">
      <fileset dir="${target.doc.api.dir}"/>
    </copy>
    <copy todir="${dist.web.dir}">
      <fileset dir="${web.dir}"/>
    </copy>

  </target>

  <!--
     ========================================================================
       Perform a code audit using CheckStyle. Only performs the audit if
       the checkstyle jar is in the Ant classpasth.
     ========================================================================
  -->
  <target name="checkstyle" depends="init,init.checkstyle,compile" 
      if="checkstyle.available"
      description="Perform a code audit using Checkstyle">

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

    <checkstyle config="./checkstyle.xml" 
        failOnViolation="true">

      <fileset dir="${src.java.dir}/j2ee-12" 
          includes="**/*.java" excludes="**/*Aspect.*">
        <selector if="j2ee12.available"/>
      </fileset>
      <fileset dir="${src.java.dir}/j2ee-13"
          includes="**/*.java" excludes="**/*Aspect.*">
        <selector if="j2ee13.available"/>
      </fileset>
      <fileset dir="${src.java.dir}/j2ee-14"
          includes="**/*.java" excludes="**/*Aspect.*">
        <selector if="j2ee14.available"/>
      </fileset>
      <fileset dir="${src.java.dir}/share-12-13-14"
          includes="**/*.java" excludes="**/*Aspect.*"/>
      <fileset dir="${src.java.dir}/share-13-14"
          includes="**/*.java" excludes="**/*Aspect.*">
        <or>
          <selector if="j2ee13.available"/>
          <selector if="j2ee14.available"/>
        </or>
      </fileset>

      <fileset dir="${src.test.dir}/j2ee-12" includes="**/*.java">
        <selector if="j2ee12.available"/>
      </fileset>
      <fileset dir="${src.test.dir}/j2ee-13" includes="**/*.java">
        <selector if="j2ee13.available"/>
      </fileset>
      <fileset dir="${src.test.dir}/j2ee-14" includes="**/*.java">
        <selector if="j2ee14.available"/>
      </fileset>
      <fileset dir="${src.test.dir}/share-12-13-14" includes="**/*.java"/>
      <fileset dir="${src.test.dir}/share-13-14" includes="**/*.java">
        <or>
          <selector if="j2ee13.available"/>
          <selector if="j2ee14.available"/>
        </or>
      </fileset>

      <formatter type="plain"/>
      <formatter type="xml"
          toFile="${target.dir}/checkstyle_results.xml"/>

      <property key="checkstyle.header.file" file="./checkstyle.license"/>

      <classpath>
        <pathelement path="${target.classes.java.dir}"/>      
      </classpath>
      
    </checkstyle>

  </target>

  <!--
     ========================================================================
       Run the Clover Swing report
     ========================================================================
  -->
  <target name="clover.swing" depends="init" if="clover.enable">

    <java classname="com.cortexeb.tools.clover.reporters.jfc.Viewer"
        fork="yes">
      <arg path="${clover.initstring}"/>
      <classpath>
        <pathelement path="${clover.jar}"/>
      </classpath>
    </java>

  </target>

  <!--
     ========================================================================
       Clean generated files (including distributables)
     ========================================================================
  -->
  <target name="clean" depends="init.display,init.properties"
    description="Clean all generated files">

    <delete dir="${target.dir}"/>
    <delete dir="${dist.dir}"/>

  </target>

</project>
