<?xml version="1.0"?>

<!--
  =============================================================================
    Cactus plugin for Maven. It uses the Cactus/Ant Integration from the 
    Cactus project.
  =============================================================================
-->

<!-- 
	Note: There is significant refactoring required for the goals cactus:test, 
	cactus:single, and cactus:match. The <cactus> tasks in each of those goals
	are mostly similar.
-->
<project 
  xmlns:j="jelly:core" 
  xmlns:doc="doc" 
  xmlns:util="jelly:util"
  xmlns:ant="jelly:ant"
  xmlns:define="jelly:define"
  xmlns:x="jelly:xml"
  xmlns:maven="jelly:maven"
  xmlns:test="test"
  xmlns:ear="ear"
  xmlns:cactustag="cactus"
  xmlns:cactus="jelly:org.apache.cactus.integration.maven.CactusTagLibrary">

  <!--
     ========================================================================
       Default goal.
     ========================================================================
  -->
  <goal name="cactus" description="Run all Cactus tests"
      prereqs="cactus:test"/>

  <!--
     ========================================================================
       Verify that the instlaled Maven has compatible versions of dependent 
       plugins.
     ========================================================================
  -->
  <goal name="cactus:check-plugin-dependencies">
    
    <maven:get plugin="maven-war-plugin" property="plugin" var="warPlugin" />
    <j:if test="${warPlugin.currentVersion.compareTo('1.6.1') lt 0}">
      <ant:fail>
        Must have WAR plugin v1.6.1 or greater installed to use this version of the Cactus plugin.
        Try: maven plugin:download -DgroupId=maven -DartifactId=maven-war-plugin -Dversion=[version]
      </ant:fail>
    </j:if>

    <maven:get plugin="maven-ejb-plugin" property="plugin" var="ejbPlugin" />
    <j:if test="${warPlugin.currentVersion.compareTo('1.6') lt 0}">
      <ant:fail>
        Must have EJB plugin v1.6 or greater installed to use this version of the Cactus plugin.
        Try: maven plugin:download -DgroupId=maven -DartifactId=maven-ejb-plugin -Dversion=[version]
      </ant:fail>
    </j:if>

    <maven:get plugin="maven-ear-plugin" property="plugin" var="earPlugin" />
    <j:if test="${earPlugin.currentVersion.compareTo('1.6.1') lt 0}">
      <ant:fail>
        Must have EAR plugin v1.6.1 or greater installed to use this version of the Cactus plugin.
        Try: maven plugin:download -DgroupId=maven -DartifactId=maven-ear-plugin -Dversion=[version]
      </ant:fail>
    </j:if>

  </goal>

  <!--
     ========================================================================
       Initializations.
     ========================================================================
  -->
  <goal name="cactus:init" prereqs="cactus:check-plugin-dependencies">

    <!-- Compute the default location of the input war file to cactify -->
    <maven:get var="warBuildDir" plugin="maven-war-plugin" property="maven.war.build.dir"/>
    <maven:get var="warFinalName" plugin="maven-war-plugin" property="maven.war.final.name"/>
    <maven:property var="cactus.src.war" name="cactus.src.war" 
        defaultValue="${warBuildDir}/${warFinalName}"/>

    <!-- Compute the default location of the input EJB file to cactify -->
    <maven:get var="ejbBuildDir" plugin="maven-ejb-plugin" property="maven.ejb.build.dir"/>
    <maven:get var="ejbFinalName" plugin="maven-ejb-plugin" property="maven.ejb.final.name"/>
    <maven:property var="cactus.src.ejb" name="cactus.src.ejb"
        defaultValue="${ejbBuildDir}/${ejbFinalName}"/>

    <!-- Not sure why but this is needed, otherwise the taskdefs below fail -->
    <!-- DEPRECATION: Should now use <maven:get>/<maven:set> instead. Once 
         everyone has moved to Maven 1.0 final, we should make the change -->
    <test:dependency-handle/>
    
    <ant:path id="cactus.classpath">
      <ant:pathelement location="${plugin.getDependencyPath('cactus:cactus-ant')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('cactus:cactus')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('commons-logging:commons-logging')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('junit:junit')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('commons-httpclient:commons-httpclient')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('aspectj:aspectjrt')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('httpunit:httpunit')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('nekohtml:nekohtml')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('xerces:xerces')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('xml-apis:xml-apis')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('rhino:js')}"/>
      <ant:pathelement location="${plugin.getDependencyPath('cargo:cargo')}"/>
    </ant:path>

    <ant:taskdef resource="cactus.tasks" classpathref="cactus.classpath"/>

    <ant:taskdef name="junit"
        classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"/>

    <ant:property name="cactus.target.classes.dir"
        value="${maven.build.dir}/test-cactus-classes"/>

    <util:available file="${cactus.src.dir}">
      <j:set var="cactusSourcePresent" value="true"/>
      <ant:path id="cactus.compile.src.set">
        <ant:pathelement location="${cactus.src.dir}"/>
      </ant:path>
    </util:available>

    <!-- The Cactus scanner automatically discovers Cactus tests -->

    <ant:path id="cactus.scanner.classpath">
      <ant:pathElement location="${maven.build.dest}"/>
      <ant:pathElement location="${cactus.target.classes.dir}"/>
      <ant:path refid="maven.dependency.classpath"/>
      <ant:path refid="cactus.classpath"/>
    </ant:path>

    <ant:dirname property="cactus.war.dir" file="${cactus.war}"/>
    <ant:basename property="cactus.war.name" file="${cactus.war}"/>

    <ant:dirname property="cactus.ejb.dir" file="${cactus.src.ejb}"/>
    <ant:basename property="cactus.ejb.name" file="${cactus.src.ejb}"/>


  </goal>

  <!--
     ========================================================================
       Preparations for executing the JUnit reports
     ========================================================================
  -->
  <goal name="cactus:init-report">

    <!-- Prepare the directories for the JUnit reports -->
    <ant:mkdir dir="${cactus.reports.dir}"/>
    
    <j:if test="${context.getVariable('cactus.home.jboss3x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/jboss3x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.orion1x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/orion1x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.orion2x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/orion2x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.resin2x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/resin2x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.resin3x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/resin3x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.tomcat4x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/tomcat4x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.tomcat5x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/tomcat5x"/>
    </j:if>
    <j:if test="${context.getVariable('cactus.home.weblogic7x') != null}">
      <ant:mkdir dir="${cactus.reports.dir}/weblogic7x"/>
    </j:if>

  </goal>

  <!--
     ========================================================================
       Compiles the Cactus tests.
     ========================================================================
  -->
  <goal name="cactus:compile" prereqs="cactus:init,java:compile"
      description="Compiles the Cactus tests">

    <ant:mkdir dir="${cactus.target.classes.dir}"/>
    
    <j:choose>
      <j:when test="${cactusSourcePresent == 'true'}">

      	<ant:javac destdir="${cactus.target.classes.dir}"
            excludes="**/package.html"
            debug="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.debug')}"
            deprecation="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.deprecation')}"
            optimize="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.optimize')}">

          <ant:src>
            <ant:path refid="cactus.compile.src.set"/>
          </ant:src>
          
          <ant:classpath>
            <ant:path refid="maven.dependency.classpath"/>
            <ant:path refid="cactus.classpath"/>
            <ant:pathelement path="${maven.build.dest}"/>
          </ant:classpath>

          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.compilerargs') != null}">
            <ant:compilerarg line="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.compilerargs')}"/>
          </j:if>
          
          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.encoding') != null}">
            <ant:setProperty name="encoding" value="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.encoding')}"/>
          </j:if>
          
          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.executable') != null}">
            <ant:setProperty name="executable" value="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.executable')}"/>
          </j:if>
          
          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.fork') != null}">
            <ant:setProperty name="fork" value="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.fork')}"/>
          </j:if>
          
          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.source') != null}">
            <ant:setProperty name="source" value="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.source')}"/>
          </j:if>
          
          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.target') != null}">
            <ant:setProperty name="target" value="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.target')}"/>
          </j:if>
          
          <j:if test="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.verbose') != null}">
            <ant:setProperty name="verbose" value="${pom.getPluginContext('maven-java-plugin').getVariable('maven.compile.verbose')}"/>
          </j:if>
        </ant:javac>

        <!-- Copy non java source files -->
        <ant:copy todir="${cactus.target.classes.dir}">
          <ant:fileset dir="${cactus.src.dir}">
            <ant:exclude name="**/*.java"/>
            <ant:exclude name="**/*.html"/>
          </ant:fileset>
        </ant:copy>

      </j:when>
      <j:otherwise>
        <ant:echo>No Cactus source files to compile.</ant:echo>
      </j:otherwise>      
    </j:choose>
    
  </goal>

  <!--
     ========================================================================
       Generate a jar containing Cactus tests classes.
     ========================================================================
  -->
  <goal name="cactus:jar" prereqs="cactus:compile"
      description="Generate a Cactus jar">

    <ant:jar
      jarfile="${maven.build.dir}/${pom.artifactId}-cactus-${pom.currentVersion}.jar"
      basedir="${cactus.target.classes.dir}"> 
      <ant:manifest>
        <ant:attribute name="Built-By" value="${user.name}"/>
        <ant:attribute name="Created-By" value="Apache Jakarta Maven"/>
        <ant:attribute name="Package" value="${pom.package}"/>
        <ant:attribute name="Build-Jdk" value="${java.version}"/>
        <ant:attribute name="Extension-Name" value="${pom.artifactId}"/>
        <ant:attribute name="Specification-Version" value="${pom.specificationVersion}"/>
        <ant:attribute name="Specification-Vendor" value="${pom.organization.name}"/>
        <ant:attribute name="Specification-Title" value="${pom.shortDescription}"/>
        <ant:attribute name="Implementation-Version" value="${pom.currentVersion}"/>
        <ant:attribute name="Implementation-Vendor" value="${pom.organization.name}"/>
        <ant:attribute name="Implementation-Vendor-Id" value="${pom.organization.identifier}"/>
      </ant:manifest>
    </ant:jar>

  </goal>

  <!--
     ========================================================================
       Cactify an application war.
     ========================================================================
  -->
  <goal name="cactus:cactifywar" prereqs="cactus:compile"
      description="Cactify the application war">
 
    <!-- Generate the war artifact -->
    <attainGoal name="${cactus.build.goal.war}"/>

    <cactifywar version="${cactus.servlet.version}" destfile="${cactus.war}">

      <!-- Only specify a source war to cactify if we are not testing EJBs.
           The reason is to prevent adding EJB sources to the cactified war,
           which can lead to problems on some containers as the EJB classes
           will be present both in the cactified war and in the ejb-jar. -->
      <j:if test="${!isEjbProject}">
        <ant:setProperty name="srcfile" value="${cactus.src.war}"/>
      </j:if>

      <!-- Add the mergewebxml attribute if it has been defined by the
      	   user -->
      <j:if test="${context.getVariable('cactus.src.mergewebxml') != null}">
        <ant:setProperty name="mergewebxml" value="${cactus.src.mergewebxml}"/>
      </j:if>        
  
      <ant:classes dir="${cactus.target.classes.dir}"/>

      <!-- Orion fails on a servlet filter that is not mapped to an actual
           resource, to trick it -->
      <filterredirector mapping="/test/filterRedirector.jsp"/>

      <!-- Files needed for JBoss -->
      <ant:classes dir="${plugin.resources}/jboss3x">
        <ant:include name="*.properties" if="cactus.home.jboss3x"/>
      </ant:classes>
      <ant:webinf dir="${plugin.resources}/jboss3x">
        <ant:include name="jboss-web.xml" if="cactus.home.jboss3x"/>
      </ant:webinf>

      <!-- Add all cactus related jars to the WEB-INF/lib dir. By default
      	   the CactifyWarTask only adds the pure Cactus jars but not
      	   related jars such as HttpUnit jars -->
      <lib file="${plugin.getDependencyPath('httpunit:httpunit')}"/>

      <!-- Add all user jars that have been marked to be included in the cactus
           test. For example, DBUnit would be a jar you would include in a 
           Cactus test but not in a runtime war -->
      <j:forEach var="artifact" items="${pom.artifacts}"> 
        <j:set var="dep" value="${artifact.dependency}"/> 
        <j:if test="${dep.getProperty('cactus.bundle')=='true'}">
          <lib file="${artifact.path}"/>
        </j:if>
      </j:forEach>

      <!-- Add cactus resources to the classes included in the war -->
      <util:tokenize var="resourcesList" delim=",">${cactus.resources.dirs}</util:tokenize>
      <j:forEach var="resourcesLabel" items="${resourcesList}">
        <j:set var="resourcesDirVarName" 
          value="cactus.resources.${resourcesLabel}.dir"/>
        <j:choose>
          <j:when test="${context.getVariable(resourcesDirVarName) != null}">
            <j:set var="includesVarName" 
              value="cactus.resources.${resourcesLabel}.includes"/>
            <j:choose>
              <j:when test="${context.getVariable(includesVarName) != null}">
                <j:set var="includes" 
                       value="${context.getVariable(includesVarName)}"/>
              </j:when>
              <j:otherwise>
                <j:set var="includes" 
                       value="**/*"/>
              </j:otherwise>
            </j:choose>
            <j:set var="excludesVarName" 
              value="cactus.resources.${resourcesLabel}.excludes"/>
            <j:choose>
              <j:when test="${context.getVariable(excludesVarName) != null}">
                <j:set var="excludes" 
                       value="${context.getVariable(excludesVarName)}"/>
              </j:when>
              <j:otherwise>
                <j:set var="excludes" 
                       value=""/>
              </j:otherwise>
            </j:choose>
  
            <util:file var="resourceFile" 
              name="${context.getVariable(resourcesDirVarName)}/"/>
            <j:choose>
              <j:when test="${resourceFile.exists() and resourceFile.isDirectory()}">
                <ant:classes dir="${context.getVariable(resourcesDirVarName)}">
                    <ant:include name="${includes}"/>
                    <ant:exclude name="${excludes}"/>
                </ant:classes>
              </j:when>
              <j:otherwise>
                <ant:echo>Skipping invalid directory ${context.getVariable(resourcesDirVarName)}.</ant:echo>
              </j:otherwise>
            </j:choose>
          </j:when>
          <j:otherwise>
              <ant:echo>You must set the directory with the ${resourcesDirVarName} property.</ant:echo>
          </j:otherwise>
        </j:choose>
      </j:forEach>
                     	   
    </cactifywar>

  </goal>

  <!--
     ========================================================================
       Execute the Cactus tests on all containers which have been defined, 
       i.e. which have a ${cactus.home.[container name]} property
       defined.
     ========================================================================
  -->
  <goal name="cactus:test" description="Run all Cactus tests" 
      prereqs="cactus:init">

    <j:choose>
      <j:when test="${cactus.test.skip}">
        <ant:echo>Not runnnig Cactus tests because cactus.test.skip is true.</ant:echo>
      </j:when>
      <j:when test="${cactusSourcePresent}">
        <j:choose>
          <j:when test="${context.getVariable('cactus.is.ear') == 'true'}">
            <attainGoal name="cactus:test-ear"/>
          </j:when>
          <j:otherwise>
            <attainGoal name="cactus:test-war"/>
          </j:otherwise>
        </j:choose>
      </j:when>
      <j:otherwise>
        <ant:echo>No Cactus tests to run.</ant:echo>
      </j:otherwise>
    </j:choose>

  </goal>

  <!--
     ========================================================================
       Execute the Cactus tests, packaging them in a WAR.
     ========================================================================
  -->
  <goal name="cactus:test-war" 
  	  prereqs="cactus:init,cactus:init-report,cactus:cactifywar"
      description="Run all Cactus tests (packaged in a WAR)">

    <cactus:scanner var="cactusScanner" classpathref="cactus.scanner.classpath">
      <fileset dir="${cactus.target.classes.dir}">
        <include name="${cactus.test.includes}"/>
        <exclude name="${cactus.test.excludes}"/>
      </fileset>
    </cactus:scanner>

    <cactus warfile="${cactus.war}" fork="yes" 
        errorproperty="cactustests.error"
        failureproperty="cactustests.failure"
        printsummary="${cactus.printsummary}">

      <!-- Pass any user-defined system properties -->
      <util:tokenize var="listOfProperties" delim=" ">${cactus.sysproperties}</util:tokenize>
      <j:forEach var="someProperty" items="${listOfProperties}">
        <ant:sysproperty key="${someProperty}" 
            value="${context.getVariable(someProperty)}"/>
      </j:forEach>     

      <!-- Configure the cactus task for logging -->
      <j:if test="${context.getVariable('cactus.logging.config.client') != null}">
        <cactusproperty server="false"
            propertiesFile="${cactus.logging.config.client}"/>
      </j:if>            
      <j:if test="${context.getVariable('cactus.logging.config.server') != null}">
        <cactusproperty server="true"
            propertiesFile="${cactus.logging.config.server}"/>
      </j:if>            

      <ant:classpath>
        <ant:pathelement path="${maven.build.dest}"/>
        <ant:pathelement location="${cactus.target.classes.dir}"/>
        <ant:path refid="maven.dependency.classpath"/>
        <ant:path refid="cactus.classpath"/>
      </ant:classpath>

      <ant:echo>Setting timeout to ${cactus.containerSet.timeout} ms</ant:echo>
      <containerset timeout="${cactus.containerSet.timeout}">

        <!-- Note: The port attribute for JBoss is only used for custom 
             configs -->
        <jboss3x if="cactus.home.jboss3x" port="${cactus.port}"
            dir="${cactus.home.jboss3x}" 
            output="${cactus.reports.dir}/jboss3x.out"
            todir="${cactus.reports.dir}/jboss3x"
            config="${cactus.jboss3x.config.name}"
            tmpdir="${cactus.jboss3x.tmp.dir}"
            jvmArgs="${cactus.jboss3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.jboss3x.config.dir') != null}">
            <ant:setProperty name="configdir" value="${cactus.jboss3x.config.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.jboss3x.jndiport') != null}">
            <ant:setProperty name="jndiport" value="${cactus.jboss3x.jndiport}"/>
          </j:if>    
        </jboss3x>

        <orion1x if="cactus.home.orion1x" port="${cactus.port}"
            dir="${cactus.home.orion1x}"
            output="${cactus.reports.dir}/orion1x.out"
            todir="${cactus.reports.dir}/orion1x"
            jvmArgs="${cactus.orion1x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
        </orion1x>

        <orion2x if="cactus.home.orion2x" port="${cactus.port}"
            dir="${cactus.home.orion2x}"
            output="${cactus.reports.dir}/orion2x.out"
            todir="${cactus.reports.dir}/orion2x"
            jvmArgs="${cactus.orion2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
        </orion2x>

        <resin2x if="cactus.home.resin2x" port="${cactus.port}"
            dir="${cactus.home.resin2x}"
            output="${cactus.reports.dir}/resin2x.out"
            todir="${cactus.reports.dir}/resin2x"
            jvmArgs="${cactus.resin2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.resin2x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin2x.config}"/>
          </j:if>        
        </resin2x>

        <resin3x if="cactus.home.resin3x" port="${cactus.port}"
            dir="${cactus.home.resin3x}"
            output="${cactus.reports.dir}/resin3x.out"
            todir="${cactus.reports.dir}/resin3x"
            jvmArgs="${cactus.resin3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.resin3x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin3x.config}"/>
          </j:if>        
        </resin3x>
        
        <tomcat4x if="cactus.home.tomcat4x" port="${cactus.port}"
            dir="${cactus.home.tomcat4x}"
            output="${cactus.reports.dir}/tomcat4x.out"
            todir="${cactus.reports.dir}/tomcat4x"
            jvmArgs="${cactus.tomcat4x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat4x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat4x.config}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat4x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat4x.tmpdir}"/>
          </j:if>             
          <j:if test="${context.getVariable('cactus.tomcat4x.conf.dir') != null}">
            <conf dir="${cactus.tomcat4x.conf.dir}"
              includes="${cactus.tomcat4x.conf.includes}"
              excludes="${cactus.tomcat4x.conf.excludes}"/>
          </j:if>
        </tomcat4x>

        <tomcat5x if="cactus.home.tomcat5x" port="${cactus.port}"
            dir="${cactus.home.tomcat5x}"
            output="${cactus.reports.dir}/tomcat5x.out"
            todir="${cactus.reports.dir}/tomcat5x"
            jvmArgs="${cactus.tomcat5x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat5x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat5x.config}"/>
          </j:if> 
          <j:if test="${context.getVariable('cactus.tomcat5x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat5x.tmpdir}"/>
          </j:if>             
          <j:if test="${context.getVariable('cactus.tomcat5x.conf.dir') != null}">
            <conf dir="${cactus.tomcat5x.conf.dir}"
              includes="${cactus.tomcat5x.conf.includes}"
              excludes="${cactus.tomcat5x.conf.excludes}"/>
          </j:if>            
        </tomcat5x>

        <weblogic7x if="cactus.home.weblogic7x" port="${cactus.port}"
            dir="${cactus.home.weblogic7x}"
            output="${cactus.reports.dir}/weblogic7x.out"
            todir="${cactus.reports.dir}/weblogic7x"
            jvmArgs="${cactus.weblogic7x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.weblogic7x.beahome') != null}">
            <ant:setProperty name="beahome" value="${cactus.weblogic7x.beahome}"/>
          </j:if> 
          <j:if test="${context.getVariable('cactus.weblogic7x.config') != null}">
            <ant:setProperty name="configxml" value="${cactus.weblogic7x.config}"/>
          </j:if>
        </weblogic7x>            

      </containerset>

      <ant:formatter type="plain" usefile="${cactus.junit.usefile}"/>
      <ant:formatter type="xml"/>

      <j:forEach var="test" items="${cactusScanner.iterator()}">
        <test name="${test}"/>
      </j:forEach>

    </cactus>

    <cactustag:junitreport/>
    
    <j:if test="${cactus.halt.on.failure}">
      <ant:fail if="cactustests.failure">At least one test had a failure!</ant:fail>
    </j:if>
    <j:if test="${cactus.halt.on.error}">
      <ant:fail if="cactustests.error">At least one test had an error!</ant:fail>
    </j:if>
        
  </goal>
  
  <!--
	 ========================================================================
	   Execute a single Cactus test on all containers which have been defined, 
	   i.e. which have a ${cactus.home.[container name]} property
	   defined.  Specify the case with -Dtestcase=my.single.TestCase
	 ========================================================================
  -->
  <goal name="cactus:single" prereqs="cactus:init,cactus:cactifywar"
      description="Run a single Cactus test">

    <j:choose>
      <j:when test="${empty(testcase)}">
	    <echo>
	      You must define the test case to run via -Dtestcase=classname
	      Example: maven -Dtestcase=MyTest cactus:single
	    </echo>
	  </j:when>
	    
    <j:otherwise>    

      <cactus warfile="${cactus.war}" fork="yes" 
          errorproperty="cactustests.error" 
          failureproperty="cactustests.failure"
          printsummary="${cactus.printsummary}">

        <!-- Pass any user-defined system properties -->
        <util:tokenize var="listOfProperties" delim=" ">${cactus.sysproperties}</util:tokenize>
        <j:forEach var="someProperty" items="${listOfProperties}">
          <ant:sysproperty key="${someProperty}" 
            value="${context.getVariable(someProperty)}"/>
        </j:forEach>

        <!-- Configure the cactus task for logging -->
        <j:if test="${context.getVariable('cactus.logging.config.client') != null}">
          <cactusproperty server="false"
              propertiesFile="${cactus.logging.config.client}"/>
        </j:if>            
        <j:if test="${context.getVariable('cactus.logging.config.server') != null}">
          <cactusproperty server="true"
              propertiesFile="${cactus.logging.config.server}"/>
        </j:if>            

        <ant:classpath>
          <ant:pathelement path="${maven.build.dest}"/>
          <ant:pathelement location="${cactus.target.classes.dir}"/>
          <ant:path refid="maven.dependency.classpath"/>
          <ant:path refid="cactus.classpath"/>
        </ant:classpath>

      <ant:echo>Setting timeout to ${cactus.containerSet.timeout} ms</ant:echo>
      <containerset timeout="${cactus.containerSet.timeout}">

        <!-- Note: The port attribute for JBoss is only used for custom 
             configs -->
        <jboss3x if="cactus.home.jboss3x" port="${cactus.port}"
            dir="${cactus.home.jboss3x}"
            output="${cactus.reports.dir}/jboss3x.out"
            todir="${cactus.reports.dir}/jboss3x"
            config="${cactus.jboss3x.config.name}"
            tmpdir="${cactus.jboss3x.tmp.dir}"
            jvmArgs="${cactus.jboss3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.jboss3x.config.dir') != null}">
            <ant:setProperty name="configdir" value="${cactus.jboss3x.config.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.jboss3x.jndiport') != null}">
            <ant:setProperty name="jndiport" value="${cactus.jboss3x.jndiport}"/>
          </j:if>    
        </jboss3x>

        <orion1x if="cactus.home.orion1x" port="${cactus.port}"
            dir="${cactus.home.orion1x}"
            output="${cactus.reports.dir}/orion1x.out"
            todir="${cactus.reports.dir}/orion1x"
            jvmArgs="${cactus.orion1x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
        </orion1x>

        <orion2x if="cactus.home.orion2x" port="${cactus.port}"
            dir="${cactus.home.orion2x}"
            output="${cactus.reports.dir}/orion2x.out"
            todir="${cactus.reports.dir}/orion2x"
            jvmArgs="${cactus.orion2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
        </orion2x>

        <resin2x if="cactus.home.resin2x" port="${cactus.port}"
            dir="${cactus.home.resin2x}"
            output="${cactus.reports.dir}/resin2x.out"
            todir="${cactus.reports.dir}/resin2x"
            jvmArgs="${cactus.resin2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.resin2x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin2x.config}"/>
          </j:if>        
        </resin2x>

        <resin3x if="cactus.home.resin3x" port="${cactus.port}"
            dir="${cactus.home.resin3x}"
            output="${cactus.reports.dir}/resin3x.out"
            todir="${cactus.reports.dir}/resin3x"
            jvmArgs="${cactus.resin3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.resin3x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin3x.config}"/>
          </j:if>        
        </resin3x>
        
        <tomcat4x if="cactus.home.tomcat4x" port="${cactus.port}"
            dir="${cactus.home.tomcat4x}"
            output="${cactus.reports.dir}/tomcat4x.out"
            todir="${cactus.reports.dir}/tomcat4x"
            jvmArgs="${cactus.tomcat4x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat4x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat4x.config}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat4x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat4x.tmpdir}"/>
          </j:if>             
          <j:if test="${context.getVariable('cactus.tomcat4x.conf.dir') != null}">
            <conf dir="${cactus.tomcat4x.conf.dir}"
              includes="${cactus.tomcat4x.conf.includes}"
              excludes="${cactus.tomcat4x.conf.excludes}"/>
          </j:if>
        </tomcat4x>

        <tomcat5x if="cactus.home.tomcat5x" port="${cactus.port}"
            dir="${cactus.home.tomcat5x}"
            output="${cactus.reports.dir}/tomcat5x.out"
            todir="${cactus.reports.dir}/tomcat5x"
            jvmArgs="${cactus.tomcat5x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat5x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat5x.config}"/>
          </j:if> 
          <j:if test="${context.getVariable('cactus.tomcat5x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat5x.tmpdir}"/>
          </j:if>             
          <j:if test="${context.getVariable('cactus.tomcat5x.conf.dir') != null}">
            <conf dir="${cactus.tomcat5x.conf.dir}"
              includes="${cactus.tomcat5x.conf.includes}"
              excludes="${cactus.tomcat5x.conf.excludes}"/>
          </j:if>            
        </tomcat5x>

        <weblogic7x if="cactus.home.weblogic7x" port="${cactus.port}"
            dir="${cactus.home.weblogic7x}"
            output="${cactus.reports.dir}/weblogic7x.out"
            todir="${cactus.reports.dir}/weblogic7x">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.weblogic7x.beahome') != null}">
            <ant:setProperty name="beahome" value="${cactus.weblogic7x.beahome}"/>
          </j:if> 
          <j:if test="${context.getVariable('cactus.weblogic7x.config') != null}">
            <ant:setProperty name="configxml" value="${cactus.weblogic7x.config}"/>
          </j:if>
        </weblogic7x>            

      </containerset>

      <ant:formatter type="plain" usefile="${cactus.junit.usefile}"/>
      <ant:formatter type="xml"/>

      <!-- Run only a single testcase passed in via -Dtestcase=my.single.TestCase-->
      <ant:test name="${testcase}"/>

     </cactus>

    </j:otherwise>         
   </j:choose>    
  </goal>

  <!--
	 ========================================================================
	   Execute a Cactus tests on all containers which have been defined, 
	   i.e. which have a ${cactus.home.[container name]} property
	   defined.  Specify the matching tests with -Dtestmatch=FooTest
	 ========================================================================
  -->
  <goal name="cactus:match" prereqs="cactus:init,cactus:cactifywar"
      description="Execute all the Cactus tests matching the given 'testmatch' variable">

    <j:choose>
      <j:when test="${empty(testmatch)}">
	    <echo>
	      You must define the test case to match via -Dtestmatch=classname
	      Example: maven -Dtestmatch=MyTest cactus:match
	    </echo>
	  </j:when>
	    
    <j:otherwise>    

      <cactus warfile="${cactus.war}" fork="yes" 
          errorproperty="cactustests.error" 
          failureproperty="cactustests.failure"
          printsummary="${cactus.printsummary}">

        <!-- Pass any user-defined system properties -->
        <util:tokenize var="listOfProperties" delim=" ">${cactus.sysproperties}</util:tokenize>
        <j:forEach var="someProperty" items="${listOfProperties}">
          <ant:sysproperty key="${someProperty}" 
            value="${context.getVariable(someProperty)}"/>
        </j:forEach>

        <!-- Configure the cactus task for logging -->
        <j:if test="${context.getVariable('cactus.logging.config.client') != null}">
          <cactusproperty server="false"
              propertiesFile="${cactus.logging.config.client}"/>
        </j:if>            
        <j:if test="${context.getVariable('cactus.logging.config.server') != null}">
          <cactusproperty server="true"
              propertiesFile="${cactus.logging.config.server}"/>
        </j:if>            

        <ant:classpath>
          <ant:pathelement path="${maven.build.dest}"/>
          <ant:pathelement location="${cactus.target.classes.dir}"/>
          <ant:path refid="maven.dependency.classpath"/>
          <ant:path refid="cactus.classpath"/>
        </ant:classpath>

      <ant:echo>Setting timeout to ${cactus.containerSet.timeout} ms</ant:echo>
      <containerset timeout="${cactus.containerSet.timeout}">

        <!-- Note: The port attribute for JBoss is only used for custom 
             configs -->
        <jboss3x if="cactus.home.jboss3x" port="${cactus.port}"
            dir="${cactus.home.jboss3x}"
            output="${cactus.reports.dir}/jboss3x.out"
            todir="${cactus.reports.dir}/jboss3x"
            config="${cactus.jboss3x.config.name}"
            tmpdir="${cactus.jboss3x.tmp.dir}"
            jvmArgs="${cactus.jboss3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.jboss3x.config.dir') != null}">
            <ant:setProperty name="configdir" value="${cactus.jboss3x.config.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.jboss3x.jndiport') != null}">
            <ant:setProperty name="jndiport" value="${cactus.jboss3x.jndiport}"/>
          </j:if>    
        </jboss3x>

        <orion1x if="cactus.home.orion1x" port="${cactus.port}"
            dir="${cactus.home.orion1x}"
            output="${cactus.reports.dir}/orion1x.out"
            todir="${cactus.reports.dir}/orion1x"
            jvmArgs="${cactus.orion1x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
        </orion1x>

        <orion2x if="cactus.home.orion2x" port="${cactus.port}"
            dir="${cactus.home.orion2x}"
            output="${cactus.reports.dir}/orion2x.out"
            todir="${cactus.reports.dir}/orion2x"
            jvmArgs="${cactus.orion2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
        </orion2x>

        <resin2x if="cactus.home.resin2x" port="${cactus.port}"
            dir="${cactus.home.resin2x}"
            output="${cactus.reports.dir}/resin2x.out"
            todir="${cactus.reports.dir}/resin2x"
            jvmArgs="${cactus.resin2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.resin2x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin2x.config}"/>
          </j:if>        
        </resin2x>

        <resin3x if="cactus.home.resin3x" port="${cactus.port}"
            dir="${cactus.home.resin3x}"
            output="${cactus.reports.dir}/resin3x.out"
            todir="${cactus.reports.dir}/resin3x"
            jvmArgs="${cactus.resin3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.resin3x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin3x.config}"/>
          </j:if>        
        </resin3x>
        
        <tomcat4x if="cactus.home.tomcat4x" port="${cactus.port}"
            dir="${cactus.home.tomcat4x}"
            output="${cactus.reports.dir}/tomcat4x.out"
            todir="${cactus.reports.dir}/tomcat4x"
            jvmArgs="${cactus.tomcat4x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat4x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat4x.config}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat4x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat4x.tmpdir}"/>
          </j:if>             
          <j:if test="${context.getVariable('cactus.tomcat4x.conf.dir') != null}">
            <conf dir="${cactus.tomcat4x.conf.dir}"
              includes="${cactus.tomcat4x.conf.includes}"
              excludes="${cactus.tomcat4x.conf.excludes}"/>
          </j:if>
        </tomcat4x>

        <tomcat5x if="cactus.home.tomcat5x" port="${cactus.port}"
            dir="${cactus.home.tomcat5x}"
            output="${cactus.reports.dir}/tomcat5x.out"
            todir="${cactus.reports.dir}/tomcat5x"
            jvmArgs="${cactus.tomcat5x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.tomcat5x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat5x.config}"/>
          </j:if> 
          <j:if test="${context.getVariable('cactus.tomcat5x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat5x.tmpdir}"/>
          </j:if>             
          <j:if test="${context.getVariable('cactus.tomcat5x.conf.dir') != null}">
            <conf dir="${cactus.tomcat5x.conf.dir}"
              includes="${cactus.tomcat5x.conf.includes}"
              excludes="${cactus.tomcat5x.conf.excludes}"/>
          </j:if>            
        </tomcat5x>

        <weblogic7x if="cactus.home.weblogic7x" port="${cactus.port}"
            dir="${cactus.home.weblogic7x}"
            output="${cactus.reports.dir}/weblogic7x.out"
            todir="${cactus.reports.dir}/weblogic7x">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.weblogic7x.beahome') != null}">
            <ant:setProperty name="beahome" value="${cactus.weblogic7x.beahome}"/>
          </j:if> 
          <j:if test="${context.getVariable('cactus.weblogic7x.config') != null}">
            <ant:setProperty name="configxml" value="${cactus.weblogic7x.config}"/>
          </j:if>
        </weblogic7x>            

      </containerset>

      <ant:formatter type="plain" usefile="${cactus.junit.usefile}"/>
      <ant:formatter type="xml"/>

      <!-- Run all testcases matched via -Dtestmatch=FooTest-->
      <ant:batchtest>
        <ant:fileset dir="${cactus.src.dir}">
          <include name="**/${testmatch}.java"/>
        </ant:fileset>
      </ant:batchtest>

     </cactus>

    </j:otherwise>         
   </j:choose>    
  </goal>

  <!--
     ========================================================================
       Initialization before cactifying the EAR
     ========================================================================
  -->
  <goal name="cactus:cactifyear-init">
    <j:set var="isEjbProject" value="true"/>
  </goal>

  <!--
     ========================================================================
       Generate a Cactified EAR
     ========================================================================
  -->
  <goal name="cactus:cactifyear" 
      prereqs="cactus:cactifyear-init,cactus:cactifywar,cactus:generate-ear-descriptor"
      description="Create and cactify an EAR">

    <!-- Generate the ejb artifact -->
    <attainGoal name="${cactus.build.goal.ejb}"/>

    <ant:mkdir dir="${maven.build.dir}"/>

    <!-- Ensure that the ear properties are loaded in memory so that they can be
         referenced -->
    <attainGoal name="ear:load"/>
         
    <ant:available property="cactus.ear.manifest.available"
        file="${pom.getPluginContext('maven-ear-plugin').getVariable('maven.ear.manifest')}"/>

    <ant:available property="cactus.ear.src.available"
        file="${pom.getPluginContext('maven-ear-plugin').getVariable('maven.ear.src')}"/>
        
    <ant:ear destfile="${cactus.ear}" 
        appxml="${maven.build.dir}/application.xml">

      <j:if test="${cactus.ear.src.available}">
        <fileset dir="${pom.getPluginContext('maven-ear-plugin').getVariable('maven.ear.src')}"
            casesensitive="false" excludes="**/META-INF/application.xml"/>
      </j:if>

      <!-- include cactus generated archives -->
      <ant:fileset dir="${cactus.war.dir}">
        <ant:include name="${cactus.war.name}"/>
      </ant:fileset>
      <ant:fileset dir="${cactus.ejb.dir}">
        <ant:include name="${cactus.ejb.name}"/>
      </ant:fileset>

      <!-- include marked dependencies -->
      <j:set var="tmpDependenciesDir" value="${maven.build.dir}/tmpEarDeps"/>
      <j:forEach var="lib" items="${pom.artifacts}">
        <j:set var="dep" value="${lib.dependency}"/>      
        <j:set var="originalPath" value="${lib.file.name}"/>      
        <j:if test="${dep.getProperty('ear.bundle')=='true' || dep.getProperty('ear.module')=='true'}">
          <ear:setPath lib="${lib}" var="bundledPath"/>
          <j:choose>
             <j:when test="${originalPath.equals(bundledPath)}">
               <ant:fileset dir="${lib.file.parent}">
                 <ant:include name="${originalPath}"/>
               </ant:fileset>
             </j:when>
             <j:otherwise>
               <echo>Dependency ${lib.file.name} will be bundled as ${bundledPath}</echo>
               <ant:copy file="${lib.file.parent}/${lib.file.name}" tofile="${tmpDependenciesDir}/${bundledPath}"/>
             </j:otherwise>
          </j:choose>
        </j:if>  
      </j:forEach>

      <!-- bundle all dependencies that were copied to a temporary dir -->
      <util:available file="${tmpDependenciesDir}">
         <ant:fileset dir="${tmpDependenciesDir}"/>
      </util:available>

      <j:if test="${cactus.ear.manifest.available}">
        <ant:setProperty name="manifest" 
            value="${pom.getPluginContext('maven-ear-plugin').getVariable('maven.ear.manifest')}"/>
      </j:if>

    </ant:ear>

  </goal>

  <!--
     ========================================================================
       Execute the Cactus tests, packaging them in an EAR.
     ========================================================================
  -->
  <goal name="cactus:test-ear"
      prereqs="cactus:init,cactus:init-report,cactus:cactifyear"
      description="Run all Cactus tests (packaged in an EAR)">

    <cactus:scanner var="cactusScanner" classpathref="cactus.scanner.classpath">
      <fileset dir="${cactus.target.classes.dir}">
        <include name="${cactus.test.includes}"/>
        <exclude name="${cactus.test.excludes}"/>
      </fileset>
    </cactus:scanner>

    <cactus earfile="${cactus.ear}" fork="yes" 
        errorproperty="cactustests.error" failureproperty="cactustests.failure"
        printsummary="${maven.cactus.printsummary}">

      <!-- Pass any user-defined system properties -->
      <util:tokenize var="listOfProperties" delim=" ">${cactus.sysproperties}</util:tokenize>
      <j:forEach var="someProperty" items="${listOfProperties}">
        <ant:sysproperty key="${someProperty}" 
            value="${context.getVariable(someProperty)}"/>
      </j:forEach>     

      <!-- Configure the cactus task for logging -->
      <j:if test="${context.getVariable('cactus.logging.config.client') != null}">
        <cactusproperty server="false"
            propertiesFile="${cactus.logging.config.client}"/>
      </j:if>            
      <j:if test="${context.getVariable('cactus.logging.config.server') != null}">
        <cactusproperty server="true"
            propertiesFile="${cactus.logging.config.server}"/>
      </j:if>            

      <ant:classpath>
        <ant:pathelement path="${maven.build.dest}"/>
        <ant:pathelement location="${cactus.target.classes.dir}"/>
        <ant:path refid="maven.dependency.classpath"/>
        <ant:path refid="cactus.classpath"/>
      </ant:classpath>

      <ant:echo>Setting timeout to ${cactus.containerSet.timeout} ms</ant:echo>
      <containerset timeout="${cactus.containerSet.timeout}">

        <!-- Note: The port attribute for JBoss is only used for custom 
             configs -->
        <jboss3x if="cactus.home.jboss3x" port="${cactus.port}"
            dir="${cactus.home.jboss3x}"
            output="${cactus.reports.dir}/jboss3x.out"
            todir="${cactus.reports.dir}/jboss3x"
            config="${cactus.jboss3x.config.name}"
            tmpdir="${cactus.jboss3x.tmp.dir}"
            jvmArgs="${cactus.jboss3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.jboss3x.config.dir') != null}">
            <ant:setProperty name="configdir" value="${cactus.jboss3x.config.dir}"/>
          </j:if>    
          <j:if test="${context.getVariable('cactus.jboss3x.jndiport') != null}">
            <ant:setProperty name="jndiport" value="${cactus.jboss3x.jndiport}"/>
          </j:if>    
        </jboss3x>

        <orion1x if="cactus.home.orion1x" port="${cactus.port}"
            dir="${cactus.home.orion1x}"
            output="${cactus.reports.dir}/orion1x.out"
            todir="${cactus.reports.dir}/orion1x"
            jvmArgs="${cactus.orion1x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
        </orion1x>

        <orion2x if="cactus.home.orion2x" port="${cactus.port}"
            dir="${cactus.home.orion2x}"
            output="${cactus.reports.dir}/orion2x.out"
            todir="${cactus.reports.dir}/orion2x"
            jvmArgs="${cactus.orion2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
        </orion2x>

        <resin2x if="cactus.home.resin2x" port="${cactus.port}"
            dir="${cactus.home.resin2x}"
            output="${cactus.reports.dir}/resin2x.out"
            todir="${cactus.reports.dir}/resin2x"
            jvmArgs="${cactus.resin2x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.resin2x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin2x.config}"/>
          </j:if>
        </resin2x>

        <resin3x if="cactus.home.resin3x" port="${cactus.port}"
            dir="${cactus.home.resin3x}"
            output="${cactus.reports.dir}/resin3x.out"
            todir="${cactus.reports.dir}/resin3x"
            jvmArgs="${cactus.resin3x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.resin3x.config') != null}">
            <ant:setProperty name="resinconf" value="${cactus.resin3x.config}"/>
          </j:if>
        </resin3x>

        <tomcat4x if="cactus.home.tomcat4x" port="${cactus.port}"
            dir="${cactus.home.tomcat4x}"
            output="${cactus.reports.dir}/tomcat4x.out"
            todir="${cactus.reports.dir}/tomcat4x"
            jvmArgs="${cactus.tomcat4x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.tomcat4x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat4x.config}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.tomcat4x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat4x.tmpdir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.tomcat4x.conf.dir') != null}">
            <conf dir="${cactus.tomcat4x.conf.dir}"
                includes="${cactus.tomcat4x.conf.includes}"
                excludes="${cactus.tomcat4x.conf.excludes}"/>
          </j:if>
        </tomcat4x>

        <tomcat5x if="cactus.home.tomcat5x" port="${cactus.port}"
            dir="${cactus.home.tomcat5x}"
            output="${cactus.reports.dir}/tomcat5x.out"
            todir="${cactus.reports.dir}/tomcat5x"
            jvmArgs="${cactus.tomcat5x.jvmArgs}">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.tomcat5x.config') != null}">
            <ant:setProperty name="serverxml" value="${cactus.tomcat5x.config}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.tomcat5x.tmpdir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tomcat5x.tmpdir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.tomcat5x.conf.dir') != null}">
           <conf dir="${cactus.tomcat5x.conf.dir}"
               includes="${cactus.tomcat5x.conf.includes}"
               excludes="${cactus.tomcat5x.conf.excludes}"/>
          </j:if>
        </tomcat5x>

        <weblogic7x if="cactus.home.weblogic7x" port="${cactus.port}"
            dir="${cactus.home.weblogic7x}"
            output="${cactus.reports.dir}/weblogic7x.out"
            todir="${cactus.reports.dir}/weblogic7x">
          <j:if test="${context.getVariable('cactus.tmp.dir') != null}">
            <ant:setProperty name="tmpdir" value="${cactus.tmp.dir}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.weblogic7x.beahome') != null}">
            <ant:setProperty name="beahome" value="${cactus.weblogic7x.beahome}"/>
          </j:if>
          <j:if test="${context.getVariable('cactus.weblogic7x.config') != null}">
            <ant:setProperty name="configxml" value="${cactus.weblogic7x.config}"/>
          </j:if>
        </weblogic7x>

      </containerset>

      <ant:formatter type="plain" usefile="${cactus.junit.usefile}"/>
      <ant:formatter type="xml"/>

      <j:forEach var="test" items="${cactusScanner.iterator()}">
        <test name="${test}"/>
      </j:forEach>

    </cactus>

    <cactustag:junitreport/>
    
    <j:if test="${cactus.halt.on.failure}">
      <ant:fail if="cactustests.failure">At least one test had a failure!</ant:fail>
    </j:if>
    <j:if test="${cactus.halt.on.error}">
      <ant:fail if="cactustests.error">At least one test had an error!</ant:fail>
    </j:if>

  </goal>

  <!--
     ========================================================================
       Creates ear descriptor - application.xml file
     ========================================================================
  -->
  <goal name="cactus:generate-ear-descriptor" prereqs="cactus:init">

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

    <ant:echo>Generating appxml file:[${maven.build.dir}/application.xml] version ${cactus.j2ee.version}</ant:echo>

    <j:file name="${maven.build.dir}/application.xml" outputMode="xml"
        prettyPrint="true" encoding="UTF-8">

      <j:switch on="${cactus.j2ee.version}">
        <j:case value="1.3">
          <x:doctype name="application" 
              publicId="-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" 
              systemId="http://java.sun.com/dtd/application_1_3.dtd"/>
        </j:case>
        <j:default>
          <x:doctype name="application" 
              publicId="-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN"
              systemId="http://java.sun.com/j2ee/dtds/application_1_2.dtd"/>
        </j:default>
      </j:switch>

      <x:element name="application">
        <x:element name="display-name">${pom.name} for Cactus</x:element>
  
        <!-- cactus war file -->
        <x:element name="module">
          <x:element name="web">
            <x:element name="web-uri">${cactus.war.name}</x:element>
            <x:element name="context-root">${pom.artifactId}</x:element>
          </x:element>
        </x:element>

        <!-- ejb module under test -->
        <x:element name="module">
          <x:element name="ejb">${cactus.ejb.name}</x:element>
        </x:element>

        <j:forEach var="lib" items="${pom.artifacts}">
          <j:set var="dep" value="${lib.dependency}"/>
          <ear:setPath lib="${lib}" var="bundledPath"/>
          <j:if test="${dep.getProperty('ear.bundle')=='true' || dep.getProperty('ear.module')=='true'}">
            <j:choose>
              <j:when test="${dep.type=='war'}">
                <x:element name="module">
                  <x:element name="web">
                    <x:element name="web-uri">${bundledPath}</x:element>
                    <x:element name="context-root">${dep.getProperty('ear.appxml.war.context-root')}</x:element>
                 </x:element>                
                </x:element>
              </j:when>
              <j:when test="${dep.type=='ejb'}">
                <x:element name="module">
                  <x:element name="ejb">${bundledPath}</x:element>                 
                </x:element>
              </j:when>       
              <j:when test="${dep.type=='jar'}">
                <j:if test="${dep.getProperty('ear.module')=='true'}">
                  <x:element name="module">  
                  <x:element name="java">${bundledPath}</x:element>                 
                  </x:element>
                </j:if>
              </j:when>
              <j:when test="${dep.type=='rar'}">
                <x:element name="module">  
                <x:element name="connector">${bundledPath}</x:element>                 
                </x:element>
              </j:when>
              <j:when test="${dep.type=='sar'}">
                <x:element name="module">
                <x:element name="connector">${bundledPath}</x:element>
                </x:element>
              </j:when>
            </j:choose> 
          </j:if>        
        </j:forEach>

      </x:element>
    </j:file>

  </goal>

  <!--
     ========================================================================
       Register a Cactus report.
     ========================================================================
  -->
  <goal name="maven-cactus-plugin:register">
    <doc:registerReport 
      name="Cactus Unit Tests"
      pluginName="cactus"
      description="Report on the results of the Cactus unit tests."
      link="cactus-report"/>
  </goal>
  
  <!--
     ========================================================================
       Deregister a Cactus report.
     ========================================================================
  -->
  <goal name="maven-cactus-plugin:deregister">
    <doc:deregisterReport name="Cactus Unit Tests"/>
  </goal>

  <!--
     ========================================================================
       Generate a Cactus report.
     ========================================================================
  -->
  <goal name="cactus:report"
    description="Generate a report from the test results">

    <j:if test="${context.getVariable('cactus.execute.during.report') == 'true'}">

      <!-- When running reports, make sure Cactus tests do not fail the 
           build -->
      <j:set var="cactus.halt.on.failure" value="false"/>
      <j:set var="cactus.halt.on.error" value="false"/>
      <attainGoal name="cactus:test"/>

    </j:if>
 
    <!-- Before running the report, check the required test suite xml file has
         been generated -->
    <util:file var="inputAsFile" 
        name="${cactus.reports.dir}/TESTS-TestSuites-Cactus.xml"/>
    <j:choose>
      <j:when test="${inputAsFile.exists()}">

        <doc:jsl
            input="${cactus.reports.dir}/TESTS-TestSuites-Cactus.xml"
            output="cactus-report.xml"
            stylesheet="${plugin.resources}/cactus.jsl"
            outputMode="xml"
            prettyPrint="true"/>

      </j:when>
      <j:otherwise>
        <ant:mkdir dir="${maven.gen.docs}"/>
        <j:file name="${maven.gen.docs}/cactus-report.xml" prettyPrint="true" 
            xmlns="cactus">
          <document>
            <properties>
              <title>Cactus Test Results</title>
            </properties>
            <body>
              <section name="Cactus test results">
                <p>
                  No Cactus results to report. Make sure the Cactus tests are
                  run before the report is generated.
                </p>
              </section>
            </body>
          </document>
        </j:file>
        <echo>You need to run Cactus tests before generating the report.</echo>
      </j:otherwise>
    </j:choose>     

  </goal>
  
  <!--
     ========================================================================
       Install the Cactus jar in the local repository.
     ========================================================================
  -->
  <goal name="cactus:jar-install" prereqs="cactus:jar"
    description="Install the Cactus jar in the local repository">
    
    <ant:echo>
    id = '${pom.artifactId}'
    groupId = '${pom.groupId}'
    artifactDirectory = '${pom.artifactDirectory}'
    </ant:echo>
    
    <ant:property name="jardir__" value="${maven.repo.local}/${pom.artifactDirectory}/jars"/>
    <ant:mkdir dir="${jardir__}"/>    
    <ant:copy 
      file="${maven.build.dir}/${pom.artifactId}-cactus-${pom.currentVersion}.jar"
      todir="${jardir__}"
      overwrite="true"/>
  </goal>

  <!--
     ========================================================================
       Useful tag libraries to promote reuse of Jelly code snippets.
     ========================================================================
  -->

  <define:taglib uri="cactus">

    <!-- Use this tag to allow this plugin to be loaded into another -->
    <!-- DEPRECATION: No longer required. Dependent plugins should now use 
         <maven:get>/<maven:set> instead. Once everyone has moved to Maven 
         1.0 final, we should make the change -->
    <define:tag name="dependency-handle">
    </define:tag>

    <!-- Consolidate the JUnit reports into a single one
         TODO: Change the Cactus stylesheet (cactus.jsl) to handle reports
    	   for several containers -->
    <define:tag name="junitreport">

      <ant:junitreport todir="${cactus.reports.dir}"
          tofile="TESTS-TestSuites-Cactus.xml">

        <j:if test="${context.getVariable('cactus.home.jboss3x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/jboss3x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.orion1x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/orion1x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.orion2x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/orion2x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.resin2x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/resin2x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.resin3x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/resin3x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.tomcat4x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/tomcat4x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.tomcat5x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/tomcat5x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

        <j:if test="${context.getVariable('cactus.home.weblogic7x') != null}">
          <ant:fileset dir="${cactus.reports.dir}/weblogic7x">
            <ant:include name="TEST-*.xml"/>
          </ant:fileset>
        </j:if>

      </ant:junitreport>
      
    </define:tag>

  </define:taglib>

</project>
