<!--
    Copyright (C) Janne Jalkanen 2001-2004.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->

<!--
    This is the Ant build file for the JSPWiki project.

    The verbosity in this file is intentional - it is also
    an example for those who don't know Ant yet that well
    and would like to learn it.

    The build file assumes the following directory structure:

    JSPWiki
    |___build.xml
    |  
    |___etc
    |   |___[jspwiki.properties and web.xml]
    |          
    |___src     
    |   |___webdocs
    |   |   |___[all .jsp files]
    |   |       
    |   |___com 
    |       |___[...and the rest of the source code files]
    |
    |___docs
    |
    |___lib
    | 
    |___tests   
        |___com 
            |___[...and the rest of the test source code]
                  
    $Id: build.xml,v 1.36 2004/11/14 13:17:18 jalkanen Exp $
-->    
       
<!--   
    First, we define the project.  We assign it a name,
    and the default action if no action is specified on the
    command line.  Also, all relative directory references
    in the rest of the project file should be calculated from
    the current directory.
-->    
<project name="JSPWiki" default="compile" basedir=".">

  <!-- This tells us which build.properties file lies.  By default, we
       use the CVS version (which is tuned to my environment), but you
       will probably want to override this from the command line. -->
  <property name="build.properties" value="build.properties" />

  <property file="${build.properties}" />
       
  <!-- This denotes the directory where the source code lies. -->
  <property name="code.src" value="src" />
       
  <!-- The class files are actually put in this directory.  It is
       a good habit to keep .class -files separate from the .java -files. -->
  <property name="code.build" value ="build" />
       
  <!-- Define a temporary directory, based on the system temporary directory,
       the user name, and the project name (defined above) -->
  <property name="tmpdir" value="${java.io.tmpdir}/${user.name}/${ant.project.name}" />

  <!-- The following three properties define the location of the
       test sources, the location of the test .class files and the
       directory where the test results are written in. -->

  <property name="tests.src" value="tests" />
  <property name="tests.build" value="tests/build" />
  <property name="tests.reports" value="tests/reports" />

  <!-- The place where the javadocs are created -->

  <property name="docs.javadoc" value="doc/javadoc" />

  <!-- The temporary installation directory where all war-files 
       are collected, for example -->
  <property name="install.fulldir" value="${tmpdir}/install" />

  <!-- The directory where the CVS sources are checked out. -->
  <property name="install.src" value="${tmpdir}/cvssrc" />

  <!-- Define the CVS properties.  These are used when building the
       source distribution.  Normally, you shouldn't have to care about these. 
   -->
  <property name="cvs.root" value=":ext:grey.ecyrd.com:/p/cvs" />
  <property name="cvs.module" value="JSPWiki" />
  <property name="cvs.tag" value="HEAD" />

  <!-- And finally, the directory where the final .zip-file is put -->
  <property name="release.dir" value="releases" />
  
  <!--  PATH DEFINITIONS -->

  <!-- The base path for compilation.  We include, of course, the
       already built files in the build-directory, and then we
       add all the jar files in the "lib" -directory. -->
  <path id="path.base">
     <pathelement path="${code.build}" />
     <fileset dir="lib">
        <include name="*.jar" />
     </fileset>
  </path>

  <!-- The path used for running tests.  We add the tests/etc directory
       to the base path defined above, since we put all the relevant
       .properties-files in tests/etc. -->
  <path id="path.tests">
      <pathelement path="${tests.src}/etc" />
      <path refid="path.base" />
  </path>

  <!-- ============================================================== -->

  <!-- Initialising, cleaning, etc. -->

  <target name="init"
          description="Initializes everything, creates directories, etc."
          depends="mkpropertyfile">
     <mkdir dir="${code.build}" />
     <mkdir dir="${tests.build}" />
     <mkdir dir="${tests.reports}" />
     <mkdir dir="${@tests.pagedir@}" />
  </target>

  <target name="mkpropertyfile"
          description="Builds the correct propertyfile from the build.properties">
     <copy file="etc/jspwiki.properties.tmpl" tofile="etc/jspwiki.properties" />
     <copy file="tests/etc/jspwiki.properties.tmpl" tofile="tests/etc/jspwiki.properties" />
     <copy file="tests/etc/jspwiki_rcs.properties.tmpl" tofile="tests/etc/jspwiki_rcs.properties" />
     <copy file="tests/etc/jspwiki_vers.properties.tmpl" tofile="tests/etc/jspwiki_vers.properties" />
     <replace file="etc/jspwiki.properties"
              replacefilterfile="${build.properties}" />
     <replace file="tests/etc/jspwiki.properties"
              replacefilterfile="${build.properties}" />
     <replace file="tests/etc/jspwiki_rcs.properties"
              replacefilterfile="${build.properties}" />
     <replace file="tests/etc/jspwiki_vers.properties"
              replacefilterfile="${build.properties}" />
  </target>

  <!-- Removes the build directory and the tests build directory -->
  <target name="clean"
          description="Cleans away all generated files.">
    <delete dir="${tests.build}" />
    <delete dir="${code.build}" />
    <delete file="etc/jspwiki.properties" />
    <delete file="tests/etc/jspwiki.properties" />
    <delete file="tests/etc/jspwiki_rcs.properties" />
    <delete file="tests/etc/jspwiki_vers.properties" />
    <delete>
      <fileset dir="." includes="**/*~" defaultexcludes="no"/>
      <fileset dir="." includes="**/#*#" defaultexcludes="no"/>
    </delete>
  </target>

  <!-- ============================================================== -->

  <!-- Compilation targets -->

  <!-- In English this means that the "init" -target must be executed
       first.  After this, the java compiler is invoked with options
       that compile every .java file in ${code.src} into .class files
       in directory ${code.build}.  The is no debugging information
       and the compiler is instructed to optimize the resulting code.

       For the classpath we use the previously defined path called
       "path.base" -->

  <target name="compile" depends="init"
          description="Builds the source code.">
    <javac srcdir="${code.src}"
           destdir="${code.build}"
           debug="on"
           optimize="off"
           deprecation="off">
      <classpath refid="path.base" />
    </javac>
  </target>

  <target name="compile-optimized" depends="clean,init"
          description="Builds the source code for distribution.">
    <javac srcdir="${code.src}"
           destdir="${code.build}"
           debug="off"
           optimize="on">
      <classpath refid="path.base" />
    </javac>
  </target>


  <!-- This is similar to above.  We use this to compile the
       tests. -->
  <target name="compiletests" depends="init,compile"
          description="Builds the test code.">
    <javac srcdir="${tests.src}"
           destdir="${tests.build}"
           debug="on">
      <classpath refid="path.base" />
    </javac>
  </target>

  <!-- Creates javadocs -->

  <!-- FIXME: Must not use constant package name! -->
  <target name="javadoc"
          description="Compiles the javadocs.">

    <mkdir dir="${docs.javadoc}" />

    <javadoc sourcepath="${code.src}"
             destdir="${docs.javadoc}"
             use="yes"
             packagenames="com.ecyrd.jspwiki,com.ecyrd.jspwiki.plugin,com.ecyrd.jspwiki.rss,com.ecyrd.jspwiki.xmlrpc,com.ecyrd.jspwiki.tags,com.ecyrd.jspwiki.providers,com.ecyrd.jspwiki.attachment,com.ecyrd.jspwiki.acl,com.ecyrd.jspwiki.auth,com.ecyrd.jspwiki.auth.modules,com.ecyrd.jspwiki.filters,com.ecyrd.jspwiki.util,com.ecyrd.jspwiki.auth.permissions,com.ecyrd.jspwiki.atom,com.ecyrd.jspwiki.forms">
      <classpath refid="path.base" />
    </javadoc>
             
  </target>

  <!-- ============================================================== -->

  <!--  Installation targets -->

  <!-- This target makes sure all the necessary directories exist
       for building the installation package. -->
  <target name="installinit">
    <mkdir dir="${install.fulldir}" />
    <delete dir="${install.src}" />
    <mkdir dir="${install.src}" />
    <delete dir="${release.dir}" />
    <mkdir dir="${release.dir}" />
  </target>

  <!--  Builds the jar of all compiled class files -->

  <target name="jar" depends="compile,installinit">
      <property name="jarfile" value="${code.build}/${ant.project.name}.jar" />

      <jar jarfile="${jarfile}">
         <fileset dir="${code.build}" includes="**/*.class" />
         <fileset dir="${code.src}" includes="com/**/*.properties" />
      </jar>

  </target>

  <target name="jar-optimized" depends="compile-optimized,installinit">
      <property name="jarfile" value="${code.build}/${ant.project.name}.jar" />

      <jar jarfile="${jarfile}">
         <fileset dir="${code.build}" includes="**/*.class" />
      </jar>

  </target>

  <!--  Builds a Web Archive - basically a JAR file which
        also contains all of the JSP pages and can be deployed
        as-is. 
        
        The archive gets put in the ${install.fulldir}.  The 
        reason for this is that this is just a temporary
        step when building the entire distribution archive.

        We include the following things:

        1) All .jar -files in the lib-directory (except servlet.jar, since
           it's gonna be provided by the servlet container anyway.)
        2) All .class-files from the build-directory
        3) Everything from the src/webdocs -directory
        4) Everything from the etc-directory go to the WEB-INF -directory
           of the WAR-file.
   -->

  <target name="war" depends="installinit,jar"
          description="Builds the WAR file for installation.">

    <property name="warfile" value="${install.fulldir}/${ant.project.name}.war" />

    <delete file="${warfile}" />

    <war warfile="${install.fulldir}/${ant.project.name}.war"
         webxml="etc/web.xml">
       <lib dir="lib" includes="*.jar" excludes="servlet.jar,junit.jar"/>
       <lib dir="${code.build}" includes="*.jar" />
       <fileset dir="${code.src}/webdocs" includes="**" />
       <webinf dir="etc" includes="**" excludes="**.tmpl" />
    </war>

  </target>


  <target name="opened-war" depends="war"
   description="Creates an opened JSPWiki war hierarhcy into the build dir.">

    <mkdir dir="${code.build}/${ant.project.name}" />
    <unzip src="${install.fulldir}/${ant.project.name}.war"
           dest="${code.build}/${ant.project.name}" />

  </target>


  <!--
       Here goes some nice Ant magic...  We build the source
       code archive by directly exporting all code from the CVS
       repository, and then zipping it to the temporary installation
       directory.

       Note that you must have your CVS set up so that it does
       not ask for a password when you're checking it out.

       If you don't have CVS access, you can't build a source 
       zip with this.  Sorry.
   -->
  <target name="srczip" depends="installinit"
          description="Builds source zip.">

    <cvs cvsRoot="${cvs.root}" 
         dest="${install.src}" 
         package="${cvs.module}" 
         command="export" 
         tag="${cvs.tag}" />

    <zip zipfile="${release.dir}/${ant.project.name}-src.zip">
        <zipfileset dir="${install.src}" />
    </zip>

    <zip zipfile="${install.fulldir}/${ant.project.name}-samplepages.zip">
        <zipfileset dir="${install.src}/${ant.project.name}/src/wikipages" />
    </zip>

  </target>

  <!-- Builds the entire distribution set.

       We build both the WAR-file and the source zip, then
       copy in some useful files and zip the whole thing
       into the release directory.

       Note that if you don't have CVS access set up, you 
       probably can't run this.
    -->
  <target name="dist" depends="installinit,srczip,war"
          description="Builds the entire distribution archive.">
     <copy file="README" todir="${install.fulldir}" />
     <copy file="ChangeLog" todir="${install.fulldir}" />
     <copy file="ReleaseNotes" todir="${install.fulldir}" />
     <copy file="doc/LICENSE" todir="${install.fulldir}" />     

     <zip zipfile="${release.dir}/${ant.project.name}-bin.zip">
        <zipfileset dir="${install.fulldir}" prefix="${ant.project.name}" />
     </zip>

  </target>

  <!-- ============================================================== -->

  <!-- Running tests -->

  <!-- This target runs the JUnit tests that are available
       under tests/.  It generates the test result files
       into the ${tests.reports} -directory, one file per
       each tested class.  The tests are generated in
       plain text, but you can easily get XML format results
       as well, just by setting the formatter, below.

       Only tests that end with "*Test.java" are included.
       This is because then you can also use a manual
       "AllTests.java" in each directory, as per the JUnit
       Cookbook.

       This runs the tests in text mode.  If you want the
       pretty GUI you probably want to write a new target.
   -->
  <target name="tests" depends="compile,compiletests"
          description="Runs the JUnit tests.">

    <junit printsummary="yes" haltonfailure="no" fork="no">
        <classpath>
           <path refid="path.tests" />
           <pathelement path="${tests.build}" />
           <pathelement path="${java.class.path}" />
        </classpath>

        <formatter type="plain" />

        <batchtest todir="${tests.reports}">
           <fileset dir="${tests.src}">
                <include name="**/*Test.java" />
                <exclude name="**/AllTest*java" />
           </fileset>
        </batchtest>
     </junit>

  </target>

  <target name="stresstests" depends="compile,compiletests"
          description="Runs the complete stress testing framework.">

    <junit printsummary="yes" haltonfailure="no" fork="no">
        <classpath>
           <path refid="path.tests" />
           <pathelement path="${tests.build}" />
           <pathelement path="${java.class.path}" />
        </classpath>

        <formatter type="plain" />

        <batchtest todir="${tests.reports}">
           <fileset dir="${tests.src}">
                <include name="**/StressTestSpeed.java" />
           </fileset>
        </batchtest>
     </junit>

  </target>

<!--  Commented out since this kills some compiles on grey.
  <target name="report" depends="compile, compiletests">

    <mkdir dir="${tests.reports}/html" />
    <junit printsummary="yes" haltonfailure="no" fork="no">
        <classpath>
           <path refid="path.tests" />
           <pathelement path="${tests.build}" />
           <pathelement path="${java.class.path}" />
        </classpath>

        <formatter type="xml" />

        <batchtest todir="${tests.reports}">
           <fileset dir="${tests.src}">
                <include name="**/*Test.java" />
                <exclude name="**/AllTest*java" />
           </fileset>
        </batchtest>
     </junit>

     <junitreport todir="${tests.reports}/html">
        <fileset dir="${tests.reports}">
           <include name="TEST-*.xml"/>
        </fileset>
        <report format="frames" todir="${tests.reports}/html"/>
     </junitreport>

  </target>
-->

  <target name="guitests" depends="compile,compiletests"
          description="Runs the tests in a pretty window.">

     <java classname="junit.swingui.TestRunner"
           fork="true">
        <classpath>
           <path refid="path.tests" />
           <pathelement path="${tests.build}" />
           <pathelement path="${java.class.path}" />
        </classpath>

        <arg value="com.ecyrd.jspwiki.AllTests" />
     </java>
           

  </target>

  <!-- ============================================================== -->

  <!-- Tomcat war pre-compilation -->
  
  <!-- This target builds a WAR file that is specially pre-compiled
       for use with Tomcat. By generating and pre-compiling JSPs
       ahead of time (instead of having Tomcat do it on-the-fly
       at runtime), we provide a slight runtime speed bump. In
       addition, in security-conscious environments this means
       that we can eliminate the need for the Jasper JSP compiler.
       
       Pre-compilation involves three steps:
       1) Generating the .java files that correspond to JSPWiki's JSPs.
          The generated classes are actually servlets.
       2) Compiling the .java files and creating a jar.file,
          which is added to the WAR's WEB-INF/lib directory
       3) Injecting servlet mappings into the WEB-INF/web.xml
          file so that requests for the JSPs are mapped to the 
          servlets generated in step 1.
          
       To use Tomcat pre-compilation, you must set the property
       "tomcat.home" in build.properties to the directory
       containing a runtime install of Tomcat. Typically this is 
       the value of environment variable CATALINA_HOME. 
       Note that tomcat.home must point to a directory that 
       resides on the machine used to build JSPWiki.
       
       NOTE: The Jasper compiler on older versions of Tomcat
       (earlier than ~June 2004) is known to have bugs 
       that prevent the Ant "jasper2" task from running
       successfully. So, you should probably try to use
       a relatively recent build.

       In addition to the pre-compilation per se, an optional
       fourth task that generates a tarball with static content
       is also run. This task is quite handy for environments that
       use Tomcat in conjunction with a front-end web server such
       as Apache (via mod_jk/jk2 or mod_proxy). This task strips
       out the static content and adds it to a separate tar file
       This can be deployed to the web server.
       
       To generate static content, set the properties
       "static.user" and "static.group" in build.properties.
       These should be set to the Unix runtime user and group
       that should own the static files, for example the
       user "apache" group and "daemon".
  -->

  <target name="war-tomcat"      depends="build-tomcat-war,staticzip" />

  <target name="war-tomcat-init" depends="init">
    <property name="war"               value="${code.build}/${ant.project.name}" />
    <property name="war.tomcat"        value="${ant.project.name}-tomcat.war" />
    <property name="tar.static"        value="${ant.project.name}-static.tar.gz" />
    <fail unless="tomcat.home">You need to supply the property "tomcat.home"!</fail>
  </target>

  <!-- Use Jasper to generate Java class files for the JSPs.
       The compile and jar 'em up. Note that Tomcat's
       Jasper task creates a web.xml "fragment" containing
       the JSP-to-servlet mappings. We need to copy this into
       our existing web.xml, which we do using a file
       copy with token substitution.                       -->

  <target name="compile-tomcat" depends="war-tomcat-init,opened-war">
    <mkdir dir="${code.build}/jsp-java" />
    <mkdir dir="${code.build}/jsp-classes"/>
    <taskdef classname="org.apache.jasper.JspC" name="jasper2">
      <classpath>
        <path id="jspc.classpath"> 
          <pathelement location="${java.home}/../lib/tools.jar" /> 
          <fileset dir="${tomcat.home}/server/lib"> 
            <include name="*.jar" /> 
          </fileset> 
          <fileset dir="${tomcat.home}/common/lib"> 
            <include name="*.jar" /> 
          </fileset> 
          <fileset dir="${tomcat.home}/bin"> 
            <include name="commons-logging-api.jar" /> 
          </fileset> 
        </path>
      </classpath> 
    </taskdef>
    <jasper2 
      validateXml="false" 
      uriroot="${war}" 
      outputDir="${code.build}/jsp-java"
      poolingEnabled="false"
      webXmlFragment="${code.build}/web-fragment.xml" />
    <javac srcdir="${code.build}/jsp-java" destdir="${code.build}/jsp-classes">
      <classpath>
        <path refid="jspc.classpath" /> 
        <path id="war.classpath"> 
          <fileset dir="${war}/WEB-INF/lib"> 
            <include name="*.jar" /> 
          </fileset> 
        </path>
      </classpath>
    </javac>
    <jar jarfile="${code.build}/jsp.jar">
      <fileset dir="${code.build}/jsp-classes" includes="**/*.class" />
    </jar>
    <echo message="Adding JSP servlet mappings to web.xml" />
    <loadfile property="generated-web.xml" srcFile="${code.build}/web-fragment.xml"/>
    <copy file="${war}/WEB-INF/web.xml" toFile="${code.build}/web-merged.xml" overwrite="true">
      <filterset begintoken="&lt;!--" endtoken="--&gt;">
        <filter token=" PLACEHOLDER FOR PRE-COMPILED JSP SERVLETS " value="${generated-web.xml}" />
      </filterset>
    </copy>
  </target>
  
  <!-- Create a new war file with the new JSP jar and amended web.xml -->
  
  <target name="build-tomcat-war" depends="compile-tomcat"
    description="Builds the WAR file for Tomcat (with pre-compiled JSPs)">
    <war warfile="${code.build}/${war.tomcat}" webxml="${code.build}/web-merged.xml" defaultexcludes="true">
      <webinf dir="${war}/WEB-INF">
        <exclude name="web.xml" />
      </webinf>
      <lib dir="${war}/WEB-INF/lib" excludes="servlet-api.jar,j2ee.jar"/>
      <lib dir="${code.build}" includes="jsp.jar" />
      <fileset dir="${war}">
        <exclude name="WEB-INF/**" />
        <exclude name="**/*.jsp" />
      </fileset>
    </war>
  </target>

  <!-- Create a tarball containing the static content.
       User must set at least ${static.user} and preferably ${static.group}. 
       If not present, the target is skipped. -->
       
  <target name="staticzip" depends="build-tomcat-war" if="static.user">
    <property name="static.group" value="${static.user}" />
    <tar destfile="${code.build}/${tar.static}" longfile="fail" compression="gzip">
      <tarfileset dir="${war}" defaultexcludes="yes"
        username="${static.user}" group="${static.group}"
        mode="550" dirmode="770">
        <include name="**/*.css" />
        <include name="**/*.gif" />
        <include name="**/*.htm" />
        <include name="**/*.html" />
        <include name="**/*.jpg" />
        <include name="**/*.js" />
        <include name="**/*.png" />
      </tarfileset>
    </tar>
  </target>
 
</project>
