Ronsen's Blog

Pre-compile JSP for Tomcat and JBOSS

Thursday, December 18, 2008

Sometimes we need to read or debug something on our pages if some errors occur. To do this we need to compile the JSP files to make us easier to find the errors.

For Tomcat server you can read the documentation from the tomcat site here. But for JBOSS server, the trick is I used the pre-compiles JSPs using Tomcat library and copied them to JBOSS working directory. Here is the ant build script:

 <?xml version="1.0" encoding="UTF-8"?>  
 <project name="JSP Precompilation" default="all" basedir=".">  
      <property name="appname" value="demo" />  
      <property name="tomcat.home" value="/opt/apache-tomcat-5.5.27" />  
      <property name="tomcat.work.dir"  
           value="${tomcat.home}/work/Catalina/localhost/${appname}" />  
      <property name="webapp.path" value="${basedir}/web" />  
      <property name="jboss.home" value="/opt/jboss-4.0.2" />  
      <property name="jboss.work.dir"  
           value="${jboss.home}/server/default/work/jboss.web/localhost/${appname}" />  
      <target name="jspc">  
           <taskdef classname="org.apache.jasper.JspC" name="jasper2">  
                <classpath id="jspc.classpath">  
                     <pathelement location="${java.home}/../lib/tools.jar" />  
                     <fileset dir="${tomcat.home}/bin">  
                          <include name="*.jar" />  
                     </fileset>  
                     <fileset dir="${tomcat.home}/server/lib">  
                          <include name="*.jar" />  
                     </fileset>  
                     <fileset dir="${tomcat.home}/common/lib">  
                          <include name="*.jar" />  
                     </fileset>  
                </classpath>  
           </taskdef>  
           <jasper2 validateXml="false" uriroot="${webapp.path}"  
                webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"  
                outputDir="${webapp.path}/WEB-INF/src" />  
      </target>  
      <target name="compilejsp" depends="jspc">  
           <mkdir dir="${webapp.path}/WEB-INF/src" />  
           <javac destdir="${webapp.path}/WEB-INF/src" optimize="off" debug="on"  
                failonerror="false" srcdir="${webapp.path}/WEB-INF/src"  
                excludes="**/*.smap" fork="true" memoryinitialsize="128m"  
                memorymaximumsize="512m">  
                <classpath>  
                     <pathelement location="${webapp.path}/WEB-INF/classes" />  
                     <fileset dir="${webapp.path}/WEB-INF/lib">  
                          <include name="*.jar" />  
                     </fileset>  
                     <pathelement location="${tomcat.home}/common/classes" />  
                     <fileset dir="${tomcat.home}/common/lib">  
                          <include name="*.jar" />  
                     </fileset>  
                     <pathelement location="${tomcat.home}/shared/classes" />  
                     <fileset dir="${tomcat.home}/shared/lib">  
                          <include name="*.jar" />  
                     </fileset>  
                     <fileset dir="${tomcat.home}/bin">  
                          <include name="*.jar" />  
                     </fileset>  
                </classpath>  
                <include name="**" />  
                <exclude name="tags/**" />  
           </javac>  
      </target>  
      <target name="deployjsp" depends="compilejsp">  
           <copy todir="${tomcat.work.dir}">  
                <fileset dir="${webapp.path}/WEB-INF/src" includes="**/*.class" />  
           </copy>  
           <copy todir="${jboss.work.dir}">  
                <fileset dir="${webapp.path}/WEB-INF/src" includes="**/*.class" />  
           </copy>  
      </target>  
      <target name="all" depends="deployjsp" />  
      <target name="cleanup">  
           <delete>  
                <fileset dir="${webapp.path}/WEB-INF/src" />  
           </delete>  
      </target>  
 </project>  

Actually, JBOSS is using Tomcat libraries so if you have time, you can search all the jar files in JBOSS directories and find the right libraries to compile the JSP files.