<project name="jetty module build" basedir="." default="package">
  <property name="jar.file" value="${module-name}-${module-version}.jar"/>
  <property name="javac.excludes" value=""/>
  <target name="package">
    <delete dir="${build.dir}"/>
    <mkdir dir="${build.dir}"/>
    
    <echo>generating javadoc for ${module-name}</echo>
    <javadoc packagenames="org.mortbay.*,javax.servlet.*"
             destdir="${javadoc.dir}"
             author="true"
             version="true"
             public="false"
             windowtitle="jetty-${module-version} API"
             doctitle="${module-name}-${module-version}"
             encoding="ISO-8859-1"
             bottom="Copyright &amp;copy; 2009 Mortbay Consulting Pty. Ltd. All Rights Reserved.">
       <classpath>
         <path refid="classpath"/>
       </classpath>
       <link href="/usr/share/doc/default-jdk-doc/api/" />
       <link href="/usr/share/doc/libgnumail-java/api/" />
       <link href="/usr/share/doc/libservlet2.5-java/api/" />
       <sourcepath>
         <pathelement path="${src.dir}"/>
       </sourcepath>
    </javadoc>
    
    <echo>compiling ${module-name}</echo>
    <javac srcdir="${src.dir}"
           classpathref="classpath"
           destdir="${build.dir}"
           deprecation="false"
           failonerror="true"
           debug="true"
           source="1.5"
           target="1.5"
           nowarn="true"
           excludes="${javac.excludes}"
           listfiles="true"/>
    
    <echo>generating ${jar.file}</echo>
    <property name="manifest.file" value="${rsrc.dir}/META-INF/MANIFEST.MF"/>
    <available file="${manifest.file}" type="file" property="manifest.file.exists"/>
    <available file="${rsrc.dir}" type="dir" property="rsrc.dir.exists"/>
    <antcall target="generate-jar-with-given-manifest"/>
    <antcall target="generate-jar-without-given-manifest"/>
    
    <echo>copying ${jar.file} to ${module-libdir}</echo>
    <copy file="${target.dir}/${jar.file}" todir="${module-libdir}"/>
    
    <available file="${config.dir}" type="dir" property="config.dir.exists"/>
    <antcall target="copy-to-etc"/>
    <antcall target="tests" inheritRefs="true"/>
  </target>
  
  <target name="generate-jar" unless="jar.main.class">
    <antcall target="jar-with-resources"/>
    <antcall target="jar"/>
  </target>
  
  <target name="generate-jar-with-given-manifest" if="manifest.file.exists">
    <antcall target="jar-with-given-manifest-and-resources"/>
    <antcall target="jar-with-given-manifest"/>
  </target>

  <target name="generate-jar-without-given-manifest" unless="manifest.file.exists">
    <antcall target="generate-jar-with-manifest"/>
    <antcall target="generate-jar"/>
  </target>

  <target name="generate-jar-with-manifest" if="jar.main.class">
    <antcall target="jar-with-manifest-and-resources"/>
    <antcall target="jar-with-manifest"/>
  </target>
  
  <target name="jar" unless="rsrc.dir.exists">
    <jar destfile="${target.dir}/${jar.file}">
      <fileset dir="${build.dir}">
        <include name="**/*"/>
      </fileset>
    </jar>
  </target>
  
  <target name="jar-with-resources" if="rsrc.dir.exists">
    <jar destfile="${target.dir}/${jar.file}">
      <fileset dir="${build.dir}">
        <include name="**/*"/>
      </fileset>
      <fileset dir="${rsrc.dir}">
        <include name="**/*"/>
      </fileset>
    </jar>
  </target>

  <target name="jar-with-manifest" unless="rsrc.dir.exists">
    <jar destfile="${target.dir}/${jar.file}">
      <fileset dir="${build.dir}">
        <include name="**/*"/>
      </fileset>
      <manifest>    
        <attribute name="Main-Class" value="${jar.main.class}"/>
      </manifest>
    </jar>
  </target>
  
  <target name="jar-with-manifest-and-resources" if="rsrc.dir.exists">
    <jar destfile="${target.dir}/${jar.file}">
      <fileset dir="${build.dir}">
        <include name="**/*"/>
      </fileset>
      <fileset dir="${rsrc.dir}">
        <include name="**/*"/>
      </fileset>
      <manifest>    
        <attribute name="Main-Class" value="${jar.main.class}"/>
      </manifest>
    </jar>
  </target>
  
  <target name="jar-with-given-manifest" unless="rsrc.dir.exists">
    <jar destfile="${target.dir}/${jar.file}" manifest="${manifest.file}">
      <fileset dir="${build.dir}">
        <include name="**/*"/>
      </fileset>
      <fileset dir="${rsrc.dir}">
        <include name="**/*"/>
      </fileset>
    </jar>
  </target>

  <target name="jar-with-given-manifest-and-resources" if="rsrc.dir.exists">
    <jar destfile="${target.dir}/${jar.file}" manifest="${manifest.file}">
      <fileset dir="${build.dir}">
        <include name="**/*"/>
      </fileset>
      <fileset dir="${rsrc.dir}">
        <include name="**/*"/>
      </fileset>
    </jar>
  </target>
  
  <target name="copy-to-etc" if="config.dir.exists">
    <echo>copying ${config.dir} to ${etc.dir} from ${module-name}</echo>
    <copy todir="${etc.dir}">
      <fileset dir="${config.dir}" excludes="**/*.svn"/>
    </copy>
  </target>

  <target name="tests" if="run-tests">
    <javac srcdir="${test.dir}"
           classpathref="classpath"
           deprecation="false"
           failonerror="true"
           debug="true"
           source="1.5"
           target="1.5"
           nowarn="true"
           listfiles="true"/>
    <junit haltonfailure="off">
       <classpath>
         <path refid="classpath"/>
         <pathelement path="${test.dir}"/>
       </classpath>
      <formatter type="plain" usefile="false"/>
      <batchtest>
        <fileset dir="${test.dir}">
          <include name="**/*Test.java"/>
          <include name="**/*TestCase.java"/>
          <exclude name="org/mortbay/jetty/servlet/AbstractSessionTest.java"/>
          <exclude name="org/mortbay/jetty/AsyncStressTest.java"/>
          <exclude name="org/mortbay/resource/*.java"/>
          <exclude name="org/mortbay/xml/XmlConfigurationTest.java"/>
        </fileset>
      </batchtest>
    </junit>
  </target>
</project>

