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


  <!-- ==================== Initialization properties ===================== -->
  <property name="ant.home" value="../jakarta-ant"/>
  <property name="debug" value="on"/>
  <property name="j2ee.home" value="../../j2ee/build/unix"/>
  <property name="jaxp" value="../jaxp-1.0.1" />
  <property name="optimize" value="true" />
  <property name="servlet.jar" value="../jakarta-servletapi/lib/servlet.jar"/>
  <property name="tomcat.build" value="../build/tomcat"/>
  <property name="tomcat.dist" value="../dist/tomcat"/>


  <!-- ======================== Copy static files ========================= -->
  <target name="prepare">

    <!-- Create destination directories -->
    <mkdir dir="${tomcat.build}"/>
    <mkdir dir="${tomcat.build}/bin"/>
    <mkdir dir="${tomcat.build}/classes"/>
    <mkdir dir="${tomcat.build}/conf"/>
    <mkdir dir="${tomcat.build}/doc"/>
    <mkdir dir="${tomcat.build}/lib"/>
    <mkdir dir="${tomcat.build}/lib/test"/>
    <mkdir dir="${tomcat.build}/lib/test/Golden"/>
    <mkdir dir="${tomcat.build}/logs"/>
    <mkdir dir="${tomcat.build}/src"/>
    <mkdir dir="${tomcat.build}/webapps"/>

    <!-- Copy executables and scripts -->
    <copy todir="${tomcat.build}/bin">
      <fileset dir="${ant.home}/bin" includes="ant*"/>
      <fileset dir="src/shell"/>
    </copy>

    <!-- Copy configuation files -->
    <copy todir="${tomcat.build}/conf">
      <fileset dir="src/etc"/>
    </copy>
    <copy tofile="${tomcat.build}/conf/build.xml"
            file="build.xml"/>

    <!-- Copy documentation and other miscellaneous files -->
    <copy todir="${tomcat.build}/doc">
      <fileset dir="src/doc"/>
    </copy>
    <copy tofile="${tomcat.build}/LICENSE" file="LICENSE"/>
    <copy tofile="${tomcat.build}/RELEASE-NOTES" file="RELEASE-NOTES"/>
    <copy tofile="${tomcat.build}/KEYS" file="KEYS"/>

    <!-- Copy library JAR files -->
    <copy tofile="${tomcat.build}/lib/ant.jar"
            file="${ant.home}/lib/ant.jar"/>
    <copy tofile="${tomcat.build}/lib/servlet.jar"
            file="${servlet.jar}"/>
    <copy tofile="${tomcat.build}/lib/jaxp.jar"
            file="${jaxp}/jaxp.jar"/>
    <copy tofile="${tomcat.build}/lib/parser.jar"
            file="${jaxp}/parser.jar"/>

    <!-- Copy golden files for the tests webapp -->
    <copy todir="${tomcat.build}/lib/test/Golden">
      <fileset dir="src/tests/share/tests/jsp/Golden"/>
    </copy>

    <!-- Fixups for line endings and executable permissions -->
    <fixcrlf srcdir="${tomcat.build}" includes="**/*.sh" cr="remove"/>
    <fixcrlf srcdir="${tomcat.build}" includes="**/*.bat" cr="add"/>
    <chmod perm="+x" file="${tomcat.build}/bin/ant"/>
    <chmod perm="+x" file="${tomcat.build}/bin/antRun"/>
    <chmod perm="+x" file="${tomcat.build}/bin/jspc.sh"/>
    <chmod perm="+x" file="${tomcat.build}/bin/startup.sh"/>
    <chmod perm="+x" file="${tomcat.build}/bin/shutdown.sh"/>
    <chmod perm="+x" file="${tomcat.build}/bin/tomcat.sh"/>
    <chmod perm="+x" file="${tomcat.build}/doc/appdev/sample/build.sh"/>

  </target>


  <!-- ============================= Build tomcat ========================= -->
  <target name="tomcat" depends="prepare">

    <!-- Determine availability of optional components -->
    <available property="jsse.present" 
               classname="com.sun.net.ssl.internal.ssl.Provider" />
    <available property="jdk12.present"
               classname="java.security.PrivilegedAction" />

    <!-- Compile the standard Tomcat components -->
    <javac srcdir="src/share" destdir="${tomcat.build}/classes"
           classpath="${servlet.jar};${jaxp}/jaxp.jar" 
           debug="${debug}" 
           optimize="${optimize}"
           deprecation="off" >
           <exclude name="**/Jdk12Interceptor.java" 
                    unless="jdk12.present"/>
           <exclude name="**/SetSecurityManager.java" 
                    unless="jdk12.present"/>
           <exclude name="**/EmbededTomcat.java" 
                    unless="jdk12.present"/>
           <exclude name="**/PolicyInterceptor.java" 
                    unless="jdk12.present"/>
           <exclude name="**/JasperLoader12.java" 
                    unless="jdk12.present"/>
           <exclude name="**/AdaptiveClassLoader12.java" 
                    unless="jdk12.present"/>
           <exclude name="**/SSLSocketFactory.java"
                    unless="jsse.present" />
    </javac>

    <!-- Copy the corresponding resource files -->
    <copy todir="${tomcat.build}/classes">
      <fileset dir="src/share">
        <include name="**/*.properties"/>
        <include name="**/*.dtd"/>
      </fileset>
    </copy>

  </target>


  <!-- ============ Build J2EE integration code =========================== -->
  <target name="j2ee" depends="tomcat">

      <!-- Compile the J2EE integration components -->
      <javac srcdir="src/j2ee" 
          optimize="${optimize}"
          destdir="${tomcat.build}/classes"
          classpath="${servlet.jar};${j2ee.home}/lib/classes;${j2ee.home}/lib/j2ee.jar" 
          debug="${debug}" />

      <!-- Copy the corresponding resource files -->
      <copy todir="${tomcat.build}/classes">
        <fileset dir="src/j2ee">
          <include name="**/*.properties"/>
          <include name="**/web.dtd"/>
          <include name="**/web.xml"/>
        </fileset>
      </copy>

  </target>


  <!-- ==================== Build all web applications ==================== -->
  <target name="webapps" depends="prepare">

    <!-- Examples context -->
    <mkdir dir="${tomcat.build}/webapps/examples"/>
    <copy todir="${tomcat.build}/webapps/examples">
      <fileset dir="src/examples"/>
    </copy>
    <javac srcdir="src/examples/WEB-INF/classes"
           optimize="${optimize}" debug="${debug}"
           destdir="${tomcat.build}/webapps/examples/WEB-INF/classes"
           classpath="${tomcat.build}/classes;${servlet.jar}"/>
    <javac srcdir="src/examples/jsp/plugin/applet"
           optimize="${optimize}" debug="${debug}"
           destdir="${tomcat.build}/webapps/examples/jsp/plugin/applet"/>

    <!-- Root context -->
    <mkdir dir="${tomcat.build}/webapps/ROOT"/>
    <copy todir="${tomcat.build}/webapps/ROOT">
      <fileset dir="src/webpages"/>
    </copy>
    <javac srcdir="src/webpages/WEB-INF/classes"
           optimize="${optimize}" debug="${debug}"
           destdir="${tomcat.build}/webapps/ROOT/WEB-INF/classes"
           classpath="${tomcat.build}/classes;${servlet.jar}"/>

    <!-- Admin context -->
    <mkdir dir="${tomcat.build}/webapps/admin"/>
    <copy todir="${tomcat.build}/webapps/admin">
      <fileset dir="src/admin"/>
    </copy>
    <javac srcdir="src/admin/WEB-INF/classes"
           optimize="${optimize}" debug="${debug}"
           destdir="${tomcat.build}/webapps/admin/WEB-INF/classes"
           classpath="${tomcat.build}/classes;${servlet.jar}"/>

    <!-- Test application -->
    <mkdir dir="${tomcat.build}/webapps/test"/>
    <copy todir="${tomcat.build}/webapps/test">
      <fileset dir="src/tests/webpages"/>
    </copy>
    <javac srcdir="src/tests/webpages/WEB-INF/classes"
           optimize="${optimize}" debug="${debug}"
           destdir="${tomcat.build}/webapps/test/WEB-INF/classes"
           classpath="${tomcat.build}/classes;${servlet.jar}"/>
  </target>


  <!-- ==================== Copy files to distribution format ============= -->
  <target name="dist" depends="main,webapps">

    <!-- Copy build files and source tree -->
    <mkdir dir="${tomcat.dist}"/>
    <copy todir="${tomcat.dist}">
      <fileset dir="${tomcat.build}"/>
    </copy>
    <copy todir="${tomcat.dist}/src/org">
      <fileset dir="src/share/org"/>
    </copy>

    <!-- Fixups for executable permissions -->
    <chmod perm="+x" file="${tomcat.dist}/bin/ant"/>
    <chmod perm="+x" file="${tomcat.dist}/bin/antRun"/>
    <chmod perm="+x" file="${tomcat.dist}/bin/jspc.sh"/>
    <chmod perm="+x" file="${tomcat.dist}/bin/startup.sh"/>
    <chmod perm="+x" file="${tomcat.dist}/bin/shutdown.sh"/>
    <chmod perm="+x" file="${tomcat.dist}/bin/tomcat.sh"/>
    <chmod perm="+x" file="${tomcat.dist}/doc/appdev/sample/build.sh"/>

    <!-- Create tomcat JAR files -->
    <jar jarfile="${tomcat.dist}/lib/webserver.jar"
         basedir="${tomcat.build}/classes" >
         <include name="org/apache/tomcat/**" />
         <exclude name="org/apache/tomcat/deployment/**" />
         <exclude name="org/apache/tomcat/j2ee/**" />
         <exclude name="org/apache/tomcat/util/XMLParser.class" />
         <exclude name="org/apache/tomcat/util/XMLTree.class" />
    </jar> 
    <jar jarfile="${tomcat.dist}/lib/jasper.jar"
         basedir="${tomcat.dist}/classes"
         includes="org/apache/jasper/**"/>
    <delete dir="${tomcat.dist}/classes"/>

    <!-- Add Tomcat internal javadoc -->
    <mkdir dir="${tomcat.dist}/webapps/ROOT/javadoc" />
    <javadoc packagenames="org.apache.tomcat.*"
             sourcepath="src/share"
             destdir="${tomcat.dist}/webapps/ROOT/javadoc"
             author="true"
             version="true"
             use="true"
             windowtitle="Tomcat Internal API"
             doctitle="Tomcat Internal"
             bottom="Copyright &#169; 2000 Apache Software Foundation. All Rights Reserved."
    />

    <!-- Create webapp WAR files -->
    <jar   jarfile="${tomcat.dist}/webapps/examples.war"
           basedir="${tomcat.dist}/webapps/examples"
           includes="**" /> 
    <delete dir="${tomcat.dist}/webapps/examples"/>

    <jar   jarfile="${tomcat.dist}/webapps/admin.war"
           basedir="${tomcat.dist}/webapps/admin"
           includes="**" /> 
    <delete dir="${tomcat.dist}/webapps/admin"/>

    <jar   jarfile="${tomcat.dist}/webapps/ROOT.war"
           basedir="${tomcat.dist}/webapps/ROOT"
           includes="**" />
    <delete dir="${tomcat.dist}/webapps/ROOT"/>

    <jar   jarfile="${tomcat.dist}/webapps/test.war"
           basedir="${tomcat.dist}/webapps/test"
           includes="**" /> 
    <delete dir="${tomcat.dist}/webapps/test"/>

  </target>


  <!-- =================== Create J2EE integration distributin ============ -->
  <target name="j2ee-dist" depends="j2ee"> 
    <jar jarfile="${tomcat.dist}/lib/tomcat-j2ee.jar"
         basedir="${tomcat.build}/classes" >
         <include name="org/apache/tomcat/deployment/**" />
         <include name="org/apache/tomcat/j2ee/**" />
         <include name="org/apache/tomcat/util/XMLParser.class" />
         <include name="org/apache/tomcat/util/XMLTree.class" />
    </jar> 
  </target>


  <!-- ================== Package distribution as a ZIP file ============== -->
  <target name="dist-zip" depends="dist">
    <zip zipfile="${Name}-${version}.zip" basedir="${tomcat.dist}" 
         includes="**"/>
  </target>


  <!-- ==================== Administrative targets  ======================= -->
  <target name="clean">
    <delete dir="${tomcat.build}"/>
    <delete dir="${tomcat.dist}"/>
  </target>
  <target name="all" depends="clean,dist"/>
  <target name="main" depends="tomcat,webapps"/>

  
  <!-- =============================== Help =============================== -->
  <target name="targets">
    <echo message=""/>
    <echo message="ANT build targets for Tomcat"/>
    <echo message=""/>
    <echo message="The following targets are available:"/>
    <echo message="  all                  Clean, then create distribution"/>
    <echo message="  clean                Clean build and dist directories"/>
    <echo message="  dist                 Create distribution"/>
    <echo message="  dist-zip             Package distribution as a ZIP"/>
    <echo message="  j2ee                 Build J2EE integration code"/>
    <echo message="  j2ee-dist            Create J2EE distribution"/>
    <echo message="  main                 (Default) Build tomcat and webapps"/>
    <echo message="  tomcat               Build Tomcat basic components"/>
    <echo message="  webapps              Build included web applications"/>
  </target>

</project>
