<!--
  =============================================================================
    Build file used by the different Cactus subprojects. It contains common
    Ant properties and common useful targets.

    $Id: build-common.xml 369154 2006-01-15 01:28:39Z felipeal $
  =============================================================================
-->

  <!-- Environment name, used to decide which properties file to load.
       Defaults to build.properties. This is used to save configuration
       files in CVS for different environments. To use a specific
       environment, start ant like this: "ant -Denv=.sample release".
       Please note the leading "." which is mandatory. -->
  <property name="env" value=""/>

  <!-- The dependencies.xml file lists all JAR files that we can possibly
       download from a Maven-style repository. The following task converts
       the information into properties so it can be accessed by this script.
       We allow the user to override the file. -->
  <xmlproperty file="${basedir}/dependencies.xml${env}" keeproot="no"
      collapseattributes="yes" semanticattributes="yes"/>
  <xmlproperty file="${user.home}/dependencies.xml${env}" keeproot="no"
      collapseattributes="yes" semanticattributes="yes"/>

  <!-- Fall back to the default configuration if the user has not specified
       anything. This allows building Cactus right away without any
       modification from the user. -->
  <xmlproperty file="${basedir}/dependencies.xml.default" keeproot="no"
      collapseattributes="yes" semanticattributes="yes"/>

  <!-- Give user a chance to override without editing this file
       (and without typing -D each time it compiles it) -->
  <property file="${base.dir}/build.properties${env}"/>
  <property file="${basedir}/build.properties${env}"/>
  <property file="${user.home}/build.properties${env}"/>

  <!-- Fall back to the default configuration if the user has not specified
       anything. This allows building Cactus right away without any
       modification from the user. -->
  <property file="${basedir}/build.properties.default"/>
  
  <!-- Project version -->
  <!-- Note: The *.major and *.minor properties are needed by the Eclipse 
  	   plugins as plugin versions can only include numbers in the format 
  	   major.minor.patch -->

  <property name="project.version.major" value="1"/>
  <property name="project.version.minor" value="7"/>
  <property name="project.version.suffix" value=".2"/>
  <property name="project.version.suffix.timestamp" value=""/>

  <property name="project.version"
    value="${project.version.major}.${project.version.minor}${project.version.suffix}${project.version.suffix.timestamp}"/>

  <!-- Used for the web site. Note: The property must not be ${project.version}
  	   as ${project.version.suffix.timestamp} is overriden by the nightly build
  	   and replaced by a timestamped value. We want the real version value to 
  	   be displayed on the web site. -->
  <property name="project.doc.version" 
    value="${project.version.major}.${project.version.minor}${project.version.suffix}"/>
  <property name="project.doc.version.previous" value="1.7"/>

  <!-- Prefix to add to all distributable files -->
  <property name="project.prefix" value="jakarta"/>
  
  <!--
     ========================================================================
       Default values for properties not defined in build.properties or in
       a higher level calling Ant script
     ========================================================================
  -->
  <property name="year" value="2000-2004"/>
  <property name="debug" value="on"/>
  <property name="optimize" value="off"/>
  <property name="deprecation" value="off"/>

  <!--
     ========================================================================
       Display configurable properties values
     ========================================================================
  -->
  <target name="init.display">

    <echo>----- ${project.name.text} ${project.version} -----</echo>
    <echo></echo>
    <echo>java.class.path = ${java.class.path}"</echo>
    <echo></echo>
    <echo>java.home = ${java.home}</echo>
    <echo>user.home = ${user.home}</echo>
    <echo>ant.home = ${ant.home}</echo>
    <echo></echo>

  </target>

  <!--
     ========================================================================
       Find out the J2EE API version
     ========================================================================
  -->
  <target name="init.j2ee"
      if="j2ee.dependant">

    <!-- TODO: Improve this. We really need to split j2ee and servlet 
    	 verifications now that we have an EJB sample project. -->

    <condition property="j2ee.api" value="14">
      <available classname="javax.servlet.ServletRequestListener">
        <classpath>
          <pathelement location="${j2ee.jar}"/>
          <pathelement location="${servlet.jar}"/>
        </classpath>
      </available>
    </condition>
    <condition property="j2ee.api" value="13">
      <available classname="javax.servlet.Filter">
        <classpath>
          <pathelement location="${j2ee.jar}"/>
          <pathelement location="${servlet.jar}"/>
        </classpath>
      </available>
    </condition>
    <condition property="j2ee.api" value="12">
      <available classname="javax.servlet.Servlet">
        <classpath>
          <pathelement location="${j2ee.jar}"/>
          <pathelement location="${servlet.jar}"/>
        </classpath>
      </available>
    </condition>

    <fail message="Unsupported J2EE version or invalid J2EE jar [${j2ee.jar}]" 
        unless="j2ee.api"/>

    <condition property="j2ee12.available">
      <equals arg1="${j2ee.api}" arg2="12"/>
    </condition>
    <condition property="j2ee13.available">
      <equals arg1="${j2ee.api}" arg2="13"/>
    </condition>
    <condition property="j2ee14.available">
      <equals arg1="${j2ee.api}" arg2="14"/>
    </condition>

    <condition property="j2ee.title" value="J2EE 1.2">
      <equals arg1="${j2ee.api}" arg2="12"/>
    </condition>
    <condition property="j2ee.title" value="J2EE 1.3">
      <equals arg1="${j2ee.api}" arg2="13"/>
    </condition>
    <condition property="j2ee.title" value="J2EE 1.4">
      <equals arg1="${j2ee.api}" arg2="14"/>
    </condition>

    <echo message="j2ee.api = ${j2ee.api}"/>

  </target>

  <!--
     ========================================================================
       Set the properties related to the target area
     ========================================================================
  -->
  <target name="init.properties.target" depends="init.j2ee">

    <property name="target.j2ee.dir"
        location="${base.dir}/target-${j2ee.api}"/>
    <condition property="target.dir" value="${target.j2ee.dir}">
      <isset property="j2ee.api"/>
    </condition>
    <property name="target.dir" location="${base.dir}/target"/>

  </target>

  <!--
     ========================================================================
       Set the properties related to the dist area
     ========================================================================
  -->
  <target name="init.properties.dist" depends="init.j2ee">

    <property name="dist.j2ee.dir"
        location="${base.dir}/dist-${j2ee.api}"/>
    <condition property="dist.dir" value="${dist.j2ee.dir}">
      <isset property="j2ee.api"/>
    </condition>
    <property name="dist.dir" location="${base.dir}/dist"/>
    <property name="dist.lib.dir" location="${dist.dir}/lib"/>
    <property name="dist.bin.dir" location="${dist.dir}/bin"/>
    <property name="dist.doc.dir" location="${dist.dir}/doc"/>
    <property name="dist.doc.api.dir" location="${dist.doc.dir}/api"/>
    <property name="dist.web.dir" location="${dist.dir}/web"/>

  </target>

  <!--
     ========================================================================
       Set the properties related to the release area
     ========================================================================
  -->
  <target name="init.properties.release" depends="init.j2ee">

    <property name="release.j2ee.dir"
        location="${base.dir}/release-${j2ee.api}"/>

    <condition property="release.dir" value="${release.j2ee.dir}">
      <isset property="j2ee.api"/>
    </condition>
    <property name="release.dir" location="${base.dir}/release"/>

    <condition property="main.release.name"
        value="${project.prefix}-${project.name.file}-${j2ee.api}-${project.version}">
      <isset property="j2ee.api"/>
    </condition>
    <property name="main.release.name" 
        value="${project.prefix}-${project.name.file}-${project.version}"/>

    <condition property="main.release.src.name"
        value="${project.prefix}-${project.name.file}-${j2ee.api}-src-${project.version}">
      <isset property="j2ee.api"/>
    </condition>
    <property name="main.release.src.name" 
        value="${project.prefix}-${project.name.file}-src-${project.version}"/>

  </target>

  <!--
     ========================================================================
       Set the properties related to the produced artifacts
     ========================================================================
  -->
  <target name="init.properties.artifact">

    <!-- The location of the Cactus framework jar -->
    <property name="cactus.jar.name"
        value="cactus-${project.version}"/>
    <property name="cactus.jar"
        location="framework/dist-${j2ee.api}/lib/${cactus.jar.name}.jar"/>

    <!-- The location of the Clover(ed) Cactus framework jar -->
    <property name="cactus.clover.jar.name"
        value="${cactus.jar.name}-clover"/>
    <property name="cactus.clover.jar"
        location="framework/dist-${j2ee.api}/lib/${cactus.clover.jar.name}.jar"/>

    <!-- The location of the web files generated by the Cactus framework
         (i.e. the JSP Redirector) -->
    <property name="cactus.framework.web" 
      location="framework/dist-${j2ee.api}/web"/>

    <!-- The Ant Integration jar and helper classes -->
    <property name="cactus.ant.jar.name"
        value="cactus-ant-${project.version}"/>
    <property name="cactus.ant.jar" 
        location="integration/ant/dist-${j2ee.api}/lib/${cactus.ant.jar.name}.jar"/>

    <!-- The location of the Clover(ed) Ant integration jar -->
    <property name="cactus.ant.clover.jar.name"
        value="${cactus.ant.jar.name}-clover"/>
    <property name="cactus.ant.clover.jar"
        location="integration/ant/dist-${j2ee.api}/lib/${cactus.ant.clover.jar.name}.jar"/>

    <!-- The Cactus Eclipse Runner Integration dist -->
    <property name="cactus.eclipse.runner.bin"
        location="integration/eclipse/org.apache.cactus.eclipse.runner/dist-${j2ee.api}/bin"/>
    
    <!-- The Cactus Eclipse Webapp Integration dist -->
    <property name="cactus.eclipse.webapp.bin"
        location="integration/eclipse/org.apache.cactus.eclipse.webapp/dist/bin"/>
    
    <!-- The Sample Servlet dist -->
    <property name="cactus.samples.servlet.bin"
        location="samples/servlet/dist-${j2ee.api}/bin"/>

    <!-- The Sample Jetty dist -->
    <property name="cactus.samples.jetty.bin"
        location="samples/jetty/dist-${j2ee.api}/bin"/>

    <!-- The Sample EJB dist -->
    <property name="cactus.samples.ejb.bin"
        location="samples/ejb/dist-${j2ee.api}/bin"/>

  </target>

  <!--
     ========================================================================
       Load all properties
     ========================================================================
  -->
  <target name="init.properties"
      depends="init.properties.target, init.properties.dist,
               init.properties.release, init.properties.artifact"/>

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

    <tstamp/>

    <!-- Filters -->
    <filter token="version" value="${project.version}"/>
    <filter token="year" value="${year}"/>
    <filter token="today" value="${TODAY}"/>

  </target>

  <!--
     ========================================================================
       Initialize Clover.
     ========================================================================
  -->
  <target name="init.clover.check" 
    depends="init.j2ee" if="clover.enable">

    <path id="clover.classpath">
      <pathelement location="${clover.jar}"/>
      <!-- Includes system classpath for jars that are in ANT_HOME/lib 
           such as aspectrt.jar, etc -->
      <pathelement path="${java.class.path}"/>
    </path>

    <condition property="clover.available">
      <and>
        <available
            classname="org.apache.tools.ant.taskdefs.CloverCompilerAdapter"
            classpathref="clover.classpath"/>
      </and>
    </condition>

    <fail unless="clover.available">Clover is enabled but not available.
Run the init.clover target with the -debug or -verbose for details</fail>

    <taskdef resource="clovertasks" classpathref="clover.classpath"/>

    <condition property="clover.and.j2ee">
      <isset property="j2ee.api"/>
    </condition>
    <condition property="clover.and.noj2ee">
      <not><isset property="j2ee.api"/></not>
    </condition>

  </target>

  <target name="init.clover.j2ee" 
      depends="init.clover.check" if="clover.and.j2ee">

    <property name="clover.initstring"
        location="${basedir}/target-${j2ee.api}/clover-coverage.db"/>
    <mkdir dir="${basedir}/target-${j2ee.api}"/>

  </target>

  <target name="init.clover.noj2ee"
      depends="init.clover.check" if="clover.and.noj2ee">    	
    <!-- Needed for the documentation project which needs to get the
       clover data for all the J2EE APIs -->
    <property name="clover.initstring.12"
        location="target-12/clover-coverage.db"/>
    <property name="clover.initstring.13"
        location="target-13/clover-coverage.db"/>

  </target>

  <target name="init.clover"
      depends="init.clover.j2ee, init.clover.noj2ee"/>

  <!--
     ========================================================================
       Initialize Checkstyle.
     ========================================================================
  -->
  <target name="init.checkstyle.check">

    <path id="checkstyle.classpath">
      <pathelement location="${antlr.jar}"/>
      <pathelement location="${checkstyle.jar}"/>
      <pathelement location="${commons.beanutils.jar}"/>
      <pathelement location="${commons.collections.jar}"/>
      <pathelement location="${commons.logging.jar}"/>
      <pathelement location="${regexp.jar}"/>
      <!-- Includes system classpath for jars that are in ANT_HOME/lib 
           such as aspectrt.jar, etc -->
      <pathelement path="${java.class.path}"/>
    </path>

    <condition property="checkstyle.available">
      <and>
        <available
            classname="antlr.Parser"
            classpathref="checkstyle.classpath"/>
        <available
            classname="org.apache.commons.collections.CollectionUtils"
            classpathref="checkstyle.classpath"/>
        <available
            classname="org.apache.commons.logging.Log"
            classpathref="checkstyle.classpath"/>
        <available
            classname="org.apache.commons.beanutils.BeanUtils"
            classpathref="checkstyle.classpath"/>
        <available
            classname="org.apache.regexp.RE"
            classpathref="checkstyle.classpath"/>
        <available
            classname="com.puppycrawl.tools.checkstyle.CheckStyleTask"
            classpathref="checkstyle.classpath"/>
      </and>
    </condition>

  </target>

  <target name="init.checkstyle"
      depends="init.checkstyle.check, init.common"
      if="checkstyle.available">

    <taskdef resource="checkstyletask.properties"
        classpathref="checkstyle.classpath"/>

  </target>
