Adding JSP compilation to the build process.

Component Java
The road to reusable Servlet components
by Attila Szegedi
Listing 3. Adding JSP compilation to the build process.


<target name="init">
  <property
     name="lib.tomcat"
     value="wherever your tomcat.jar is"/>
  <property
     name="lib.jasper"
     value="wherever your jasper.jar is"/>
  <property
     name="lib.servlet"
     value="wherever your servlet.jar is"/>
  <property
     name="lib.sunxmlparser"
     value="wherever your parser.jar is"/>

  <property
     name="srcdir"
     value="${basedir}/src"/>
  <property
     name="jspdir"
     value="${basedir}/jsp"/>

  <property
     name="builddir" value="${basedir}/build"/>
     <property name="jspcdir"  value="${builddir}/jspc"/>
     <property name="classdir" value="${builddir}/classes"/>
     <property name="compile.classpath" value="${lib.servlet};${lib.jasper}"/>
    <property name="jspmapfile"
value="${jspcdir}/WEB-INF/org.foo.myapp.jsp.ent"/>
    <property name="jarfile" value="${builddir}/org.foo.myapp.jsp.jar"/>

    <uptodate property="jspc.notRequired"
targetfile="${jarfile}">
       <srcfiles dir= "${jspdir}"
includes="**/*.jsp" />
    </uptodate>
  </target>

  <target name="jspc" depends="init" unless="jspc.notRequired">
     <mkdir dir="${jspcdir}"/>
     <mkdir dir="${jspcdir}/WEB-INF"/>

     <copy toDir="${jspcdir}">
        <fileset dir="${jspdir}"/>
     </copy>

     <java
        classname="org.apache.jasper.JspC"
        classpath="
          ${lib.tomcat};
          ${lib.jasper};
          ${lib.servlet};
          ${lib.sunxmlparser}"
     >
        <arg line="-p org.foo.myapp.jsp"/>
        <arg line="-d ${jspcdir}"/>
        <arg line="-webinc ${jspmapfile}"/>
        <arg line="-uriroot ${jspcdir}"/>
        <arg line="-webapp ${jspcdir}"/>
     </java>
     <replace file="${jspmapfile}" token="\" value="/"/>
  </target>

  <target name="compile" depends="jspc">
     <mkdir dir="${classdir}"/>
     <mkdir dir="${jspcdir}"/>
     <javac
       destdir="${classdir}"
       classpath="${compile.classpath}"
     >
       <src path="${srcdir}"/>
       <src path="${jspcdir}"/>
       <include name="**/*.java"/>
     </javac>
  </target>

  <target name="jar" depends="compile">
     <jar jarfile="${jarfile}"
           basedir="${classdir}"
    />
  </target>

About the Author

Attila Szegedi is the lead developer at Scriptum, a company located in Szeged, Hungary, specializing in information retrieval and data cleansing technologies. Attila may be contacted at [email protected].