<?xml version="1.0"?>
<project name="Tomcat Lite" default="tomcat-lite.jar"
         xmlns:ivy="antlib:org.apache.ivy.ant"
         basedir=".">

    <property file="${user.home}/build.properties" />
    <property file="${basedir}/build.properties" />
    <property file="${basedir}/build.properties.default" />

    <property name="tomcat.base" value="${basedir}/../.." />
    <property name="tomcat.src" value="${tomcat.base}/java" />
    <property name="tomcat.build" value="${tomcat.base}/output/build" />

    <property name="tomcat.lite.src" value="${basedir}/" />
    <property name="classes" value="${basedir}/target/tomcat-lite/classes" />
    <property name="test-classes" value="${basedir}/target/tomcat-lite/test-classes" />
    <property name="http.test-classes" value="${basedir}/target/tomcat-lite/http-test-classes" />
    <property name="jar.dir" value="${basedir}/target/tomcat-lite/" />
    <property name="MAIN" value="org.apache.tomcat.integration.simple.Main"/>
    <property name="compile.source" value="1.6"/>
    <property name="compile.debug" value="true"/>

    <!-- All Ivy downloads -->
    <path id='lite-classpath'>
        <fileset dir='target/lib' includes="*.jar" />
    </path>

    <path id='head-classpath'>
        <pathelement location="${tomcat.build}/../classes" />
        <pathelement location="target/lib/asm.jar" />
        <pathelement location="target/lib/asm-tree.jar" />
        <pathelement location="target/lib/junit.jar"/>
        <pathelement location="target/lib/commons-codec.jar"/>
    </path>

    <target name="http" depends="http.compile,http.test,http.pack"/>

    <target name="http.compile"
           description="Compile only the HTTP connector classes, no servlets">
        <mkdir dir="${classes}"/>
        <javac destdir="${classes}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               source="${compile.source}"
               encoding="ISO-8859-1">
            <classpath refid="lite-classpath" />
            <src path="${tomcat.lite.src}/java" />
            <classpath refid="head-classpath" />
            <exclude name="org/apache/tomcat/servlets/**"/>
            <exclude name="org/apache/tomcat/lite/servlet/**"/>
        </javac>
        <copy todir="${classes}">
            <fileset dir="${tomcat.lite.src}/java"
                includes="**/*.properties **/*.xml" />
        </copy>
    </target>

    <target name="http.test.compile"
           description="Test only the HTTP connector classes, no servlets">
        <mkdir dir="${http.test-classes}"/>
        <javac destdir="${http.test-classes}"
                debug="${compile.debug}"
                deprecation="${compile.deprecation}"
                source="${compile.source}"
                encoding="ISO-8859-1"
         >
             <classpath refid="lite-classpath" />
             <classpath refid="head-classpath" />
             <classpath path="${classes}" />
             <src path="${tomcat.lite.src}/test" />
            <exclude name="org/apache/tomcat/lite/servlet/**"/>
            <exclude name="org/apache/coyote/lite/**"/>
            <exclude name="org/apache/tomcat/test/watchdog/**"/>
         </javac>
        <copy todir="${http.test-classes}">
            <fileset dir="${tomcat.lite.src}/test"
                includes="**/*.properties **/*.xml  **/*.bin **/*.keystore **/*.cert **/*.der" />
        </copy>
   </target>

      <target name="http.test" depends="http.test.compile" >
          <!-- also: perTest(default) -->
          <junit printsummary="on" fork="once"
                    timeout="600000" maxmemory="1G" outputtoformatters="no"
                    >
              <classpath refid="lite-classpath" />
              <classpath refid="head-classpath" />
              <classpath path="${http.test-classes}" />
              <classpath path="${classes}" />

            <formatter type="brief" usefile="false" />

            <batchtest>
              <fileset dir="test" >
                 <!-- Include all by default -->
                  <include name="**/*Test.java" />
                  <include name="**/*Tests.java" />
                 <!-- Exclude TestAll ortherwise there will be duplicated -->
                 <exclude name="**/TestAll.java" />
                 <!-- Exclude helper classes -->
                 <exclude name="**/Tester*.java" />

                     <exclude name="org/apache/tomcat/lite/servlet/**"/>
                     <exclude name="org/apache/coyote/lite/**"/>
                     <exclude name="org/apache/tomcat/test/watchdog/**"/>
              </fileset>
             </batchtest>
              </junit>
    </target>

    <target name="runtest" depends="http.test.compile">
        <junit printsummary="withOutAndErr" fork="no" dir="${tomcat.base}">
            <sysproperty key="tests" value="${tests}"/>
            <classpath refid="lite-classpath" />
            <classpath refid="head-classpath" />
            <classpath path="${http.test-classes}" />
            <classpath path="${classes}" />

            <formatter type="plain" usefile="false" />

          <batchtest>
            <fileset dir="test" >
                <include name="**/${test}.java" />
            </fileset>
           </batchtest>
            </junit>

     </target>

    <target name="http.pack"
           description="Pack the HTTP client and connector">
    </target>

    <target name="compile"
           description="Build all classes against tomcat head.">
        <mkdir dir="${classes}"/>
        <javac destdir="${classes}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               source="${compile.source}"
               encoding="ISO-8859-1">
            <classpath refid="lite-classpath" />
            <src path="${tomcat.lite.src}/java" />
            <classpath refid="head-classpath" />
            <exclude name="**/ServletApi25.java"/>
        </javac>
        <copy todir="${classes}">
            <fileset dir="${tomcat.lite.src}/java"
                includes="**/*.properties **/*.xml" />
        </copy>
    </target>

    <target name="compile25"
        description="Build against 2.5 servlet API, similar with the maven">
        <mkdir dir="${classes}" />
        <javac destdir="${classes}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               source="${compile.source}"
               encoding="ISO-8859-1">
            <classpath refid="lite-classpath" />
            <src path="${tomcat.lite.src}/java" />
            <exclude name="**/ServletApi30.java"/>
            <exclude name="org/apache/tomcat/coyote/servlet/*.java"/>
        </javac>
        <copy todir="${classes}">
            <fileset dir="${tomcat.lite.src}/java"
                includes="**/*.properties **/*.xml" />
        </copy>
    </target>

    <target name="test" depends="test30,test25"/>

    <target name="test30" depends="compile">
        <mkdir dir="${test-classes}"/>
        <javac destdir="${test-classes}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               source="${compile.source}"
               encoding="ISO-8859-1"
        >
            <classpath refid="lite-classpath" />
            <classpath refid="head-classpath" />
            <classpath path="${classes}" />
            <src path="${tomcat.lite.src}/test" />
        </javac>
        <copy todir="${test-classes}">
            <fileset dir="${tomcat.lite.src}/test"
                includes="**/*.properties **/*.xml  **/*.keystore" />
        </copy>

        <!-- Need to run it in tomcat to find output/build/webapps -->
        <junit printsummary="yes" fork="yes" dir="${tomcat.base}"
            >
            <classpath refid="lite-classpath" />
            <classpath refid="head-classpath" />
            <classpath path="${test-classes}" />
            <classpath path="${classes}" />

          <formatter type="plain" usefile="false" />

          <batchtest>
            <fileset dir="test" >
              <!-- Include all by default -->
                <include name="**/*Test.java" />
                <include name="**/*Tests.java" />
              <!-- Exclude TestAll ortherwise there will be duplicated -->
              <exclude name="**/TestAll.java" />
              <!-- Exclude helper classes -->
              <exclude name="**/Tester*.java" />
            </fileset>
           </batchtest>
            </junit>
    </target>

    <target name="test25" depends="compile25">
        <mkdir dir="${test-classes}"/>
        <javac destdir="${test-classes}"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               source="${compile.source}"
               encoding="ISO-8859-1"
        >
            <classpath refid="lite-classpath" />
            <classpath path="${classes}" />
            <classpath path="target/lib/junit.jar"/>
            <classpath path="target/lib/commons-codec.jar"/>
            <src path="${tomcat.lite.src}/test" />
            <exclude name="org/apache/coyote/**"/>
        </javac>
        <copy todir="${test-classes}">
            <fileset dir="${tomcat.lite.src}/test"
                includes="**/*.properties **/*.xml **/*.keystore" />
        </copy>

        <junit printsummary="yes" fork="yes">
            <classpath refid="lite-classpath" />
            <classpath path="${test-classes}" />
            <classpath path="${classes}" />

          <formatter type="plain" usefile="false" />

          <batchtest>
            <fileset dir="test" >
                <exclude name="org/apache/coyote/**" />
              <!-- Include all by default -->
              <include name="**/*Test.java" />
              <include name="**/*Tests.java" />
              <!-- Exclude TestAll ortherwise there will be duplicated -->
                <exclude name="**/TestAll.java" />
              <!-- Exclude helper classes -->
              <exclude name="**/Tester*.java" />
            </fileset>
           </batchtest>
            </junit>
    </target>

    <target name="clean">
        <delete dir="${classes}" includes="**" />
        <delete dir="${jar.dir}" includes="**" />
        <delete dir="${jar.dir}/jar" includes="**" />
    </target>

    <target name="tomcat-lite.jar" depends="compile,pack_tomcat-lite.jar" />

    <target name="pack_tomcat-lite.jar">
        <mkdir dir="${jar.dir}/jar" />
        <jar destfile="${jar.dir}/jar/tomcat-lite.jar">
            <manifest>
                <attribute name="Main-Class" value="${MAIN}"/>
            </manifest>
            <fileset dir="${classes}">
                <include name ="org/apache/tomcat/lite/**" />
                <include name ="org/apache/tomcat/servlets/**" />
            </fileset>
        </jar>
    </target>

    <target name="run">
        <java jar="${jar.dir}/tomcat-lite.jar"/>
    </target>

    <!-- Boilreplate for dependencies -->

    <property name="ivy.install.version" value="2.1.0" />
    <condition property="ivy.home" value="${env.IVY_HOME}">
        <isset property="env.IVY_HOME" />
    </condition>
    <property name="ivy.home" value="${basedir}/target/ivy" />
    <property name="ivy.jar.dir" value="${ivy.home}/lib" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
    <available file="${ivy.jar.file}" property="ivy.exist"/>

    <target name="download-ivy" unless="ivy.exist">
        <mkdir dir="${ivy.jar.dir}"/>
        <get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
                 dest="${ivy.jar.file}" usetimestamp="true"/>
    </target>

    <target name="init-ivy" depends="download-ivy">
        <!-- try to load ivy here from ivy home, in case the user has not already dropped
                  it into ant's lib dir (note that the latter copy will always take precedence).
                  We will not fail as long as local lib dir exists (it may be empty) and
                  ivy is in at least one of ant's lib dir or the local lib dir. -->
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>

        </path>
        <taskdef resource="org/apache/ivy/ant/antlib.xml"
                     uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
    </target>

    <target name="download" depends="init-ivy">
        <mkdir dir="target/lib"/>
        <ivy:resolve file="pom.xml" conf="compile" />
        <ivy:retrieve pattern="target/lib/[artifact].[ext]"/>
    </target>

</project>
