3 Replies Latest reply on May 13, 2004 11:58 AM by jae

    Compiling JSP

    rfn Newbie

      Could someone send me a sample ant script for compile JSP's for Jboss/Tomcat?

        • 1. Re: Compiling JSP
          Darran Lofthouse Master

          How about putting a little bit of effort in yourself.

          What is wrong with reading the manual?

          • 2. Re: Compiling JSP
            Jim Doble Newbie

            I just recently figured out how to do this. I am using JBoss 3.2.3 configured with Tomcat 5.0. Here is a portion of the ant script I ended up using:

             <java className="org.apache.jasper.JspC" fork="yes"
             failonerror="false" errorproperty="errors">
             <classpath>
             <path refid="jsp-class-path"/>
             <pathelement location="${tools-dir}/linux/jdk/lib/tools.jar"/>
             <pathelement location="${tools-dir}/ant/lib/ant.jar"/>
             </classpath>
             <arg value="-die1"/>
             <arg value="-uriroot"/>
             <arg value="${basedir}/web"/>
             <arg value="-webinc"/>
             <arg value="${basedir}/insert-web.xml"/>
             <arg value="-d"/>
             <arg value="${jsp-src-dir}"/>
             <arg value="-p"/>
             <arg value="${jsp-src-package}"/>
             </java>
            
             <condition property="errorsFound">
             <not>
             <equals arg1="${errors}" arg2=""/>
             </not>
             </condition>
            
             <fail message="${errors}" if="errorsFound"/>
            

            Some notes about what is going on here:

            1) There are a number of ways to invoke Jasper, including the jspc ant tag, but I had problems with the jspc tag (Jasper complained that it was giving it an invalid parameter) so I decided to just invoke the class via Java.

            2) The failonerror option didn't work for me (if I set it to true, the build would continue even if Jasper had errors), so I use the errorproperty to put the stderr output into a property which I test after Jasper returns.

            3) You will need to put together your own class path. Some of the jars you are likely to need include: jasper-runtime.jar, jasper-compiler.jar, jsp-api.jar, servlet-api.jar, commons-logging.jar, commons-el.jar, ant.jar, and tools.jar (from the JDK). You may also need other jars depending on what you are doing.

            4) The -uriroot parameter needs to point to a directory that contains your jsps and your WEB-INF directory. There needs to be a web.xml file in the WEB-INF directory.

            5) The -webinc parameter specifies the name of a file where Jasper should write servlet and servlet-mapping definitions. The content of this file will need to be inserted into your web.xml file (see below).

            6) The -d parameter specifies the directory where you want the generated .java files to be written.

            7) The -p parameter specifies the package name you want for your generated classes. Make sure you specify different package names for different web apps, or if you have multiple web apps that use the same jsp names, you will get weird results (i.e. you need to make sure the generated classes are unique).

            When this is complete, you will have a bunch of generated .java files, and another file containing the generated servlet and servlet-mapping definitions. The next thing you need to do is compile the .java files. Here is what I use.

             <javac destdir="${build-dir}" optimize="off" debug="on"
             failonerror="false" srcdir="${jsp-src-dir}">
             <classpath>
             <path refid="jsp-class-path"/>
             </classpath>
             </javac>
            


            The other thing you need to do before you package up your war file is to merge the generated servlet and servlet -mapping definitions into your web.xml file. You can use the ant copy tag with a filterset to do this. First, put a place-holder tag something like the following into your web.xml file, right after the last servlet or servlet-mapping definition you already have (or in the place where these definitions would usually go if you don't already have any).
             <!-- JspC-generated servlet and servlet-mapping definitions will be inserted here -->
             @JSPC_GENERATED@
            

            Then you need to do the copy. Here is my ant tag for that:
             <target name="merge-web-xml">
            
             <loadfile property="insert-web-xml" srcfile="insert-web.xml"/>
            
             <copy file="web/WEB-INF/web.xml" tofile="merged-web.xml" overwrite="true">
             <filterset>
             <filter token="JSPC_GENERATED" value="${insert-web-xml}"/>
             </filterset>
             </copy>
            
             </target>
            

            When you build your war, put the merged-web.xml file in the place of the web.xml file. For example:
             <jar jarfile="${archive-file}">
             <zipfileset file="merged-web.xml" fullpath="WEB-INF/web.xml"/>
             <zipfileset dir="${build-dir}" prefix="WEB-INF/classes"/>
             <zipfileset dir="${lib-tlds-dir}/META-INF" prefix="WEB-INF">
             <include name="*.tld"/>
             </zipfileset>
             <zipfileset dir="${lib-tlds-dir}/META-INF/tlds" prefix="WEB-INF">
             <include name="*.tld"/>
             </zipfileset>
             </jar>
            


            Note that I copy the tlds I am using (previously extracted from various library jars) into the WEB-INF directory, because JBoss doesn't seem to find them if I don't.

            I can't say that this is the best way to pre-compile your JSPs, but it does seem to work.

            • 3. Re: Compiling JSP
              jae Master

              one thing i've noticed when trying to pre-compile jsps is that jasper has a limit on how many files it can accept (i'm not sure if it's a file # limitiation, or a problem w/ the length of the path) but if you have a lot of jsps, you may need to break them up into chunks to precompile them.

              you also have the option to specify how many of a given jsp should be loaded upon startup. (sorry, i don't remember the exact xml syntax, but you should be able to find it easily enough).