You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by aparna bejugam <ap...@gmail.com> on 2011/10/03 23:41:49 UTC

Pre Compiling jsp

Hi All,

Can some one tell me where to put the jsp precompilation build
file(build.xml) in the Tomcat 7 structure.
I have a web application called myapp which is inside webapps folder and has
bunch of jsp files, I want to precompile them using the following script.

<project name="Webapp Precompilation" default="all" basedir="../../../">

<property name="tomcat.home" value="${basedir}"/>
  <condition property="java.home" value="/usr/local/java" else="C:/Program
Files/java/jdk1.6.0_02" >
               <os name="Linux"/>
    </condition>

 <property name="webapp.path" value="${tomcat.home}/webapps/myapp"/>
 <property name="outputdir" value="${webapp.path}/build"/>
 <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}/lib">
     <include name="*.jar"/>
</fileset>
 </classpath>
</taskdef>

<jasper2
     validateXml="false"
  uriroot="${webapp.path}"
 webxml="${outputdir}/WEB-INF/web.xml"
  webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"
 outputDir="${outputdir}/WEB-INF/src">
 </jasper2>

   <java classname="org.apache.jasper.JspC" fork="true"
dir="${outputdir}/WEB-INF/src">
 <classpath refid="jspc.classpath" />
<arg line="-compile"/>
 <arg line="-l"/>
<arg line="-s"/>
 <arg line="-webapp ${webapp.path}"/>
<arg line="-source ${java.home}"/>
 </java>
</target>

<target name="compile">

<mkdir dir="${outputdir}/WEB-INF/classes"/>
     <javac destdir="${outputdir}/WEB-INF/classes"
   optimize="off"
    debug="on" failonerror="false"
   srcdir="${outputdir}/WEB-INF/src"
    excludes="  **/*.smap">

 <classpath>
<fileset dir="${tomcat.home}/lib">
    <include name="*.jar"/>
</fileset>

<fileset dir="${tomcat.home}/bin">
 <include name="*.jar"/>
</fileset>
 </classpath>
<include name="**" />
 <exclude name="tags/**" />
</javac>

  </target>

   <target name="clean">
 <delete dir="${outputdir}/WEB-INF"/>
  </target>

  <target name="all" depends="jspc,compile,clean">
  </target>

 </project>

Right now I have this build file called build.xml inside the build folder in
myapp. So final structure is myapp has bunch of jsp files and a build folder
with build.xml to precompile the jsps.
When I run the above build.xml using ant I get the following error.


BUILD FAILED
C:\apache-tomcat-7.0.16\webapps\myapp\build\build.xml:29:
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
        at java.util.Vector.get(Vector.java:694)
        at org.apache.jasper.JspC.execute(JspC.java:1286)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.TaskAdapter.execute(TaskAdapter.java:154)
        at
org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:390)
        at org.apache.tools.ant.Target.performTasks(Target.java:411)
        at
org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
        at
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
        at org.apache.tools.ant.Main.runBuild(Main.java:809)
        at org.apache.tools.ant.Main.startAnt(Main.java:217)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

Total time: 1 second

I have debugged the jasper.jar code and identified that the build.xml is not
finding the jsp files. Please correct me if I'm wrong .
So my question what is wrong with my build.xml and why is that jasper 2 task
not finding the jsp files and how to tell the jasper2 task about the jsp
files.


Thanks in Advance!!!
Please Help me
I am stuck and eagerly waiting for the reply!!

Re: Pre Compiling jsp

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aparna,

On 10/3/2011 5:41 PM, aparna bejugam wrote:
> Can some one tell me where to put the jsp precompilation build 
> file(build.xml) in the Tomcat 7 structure.

You should not have to put your build.xml anywhere in the Tomcat 7
structure. Instead, you should point your build.xml file at the Tomcat
7 base installation and it should figure everything out.

> I have a web application called myapp which is inside webapps
> folder and has bunch of jsp files, I want to precompile them using
> the following script.
> 
> <project name="Webapp Precompilation" default="all"
> basedir="../../../">
> 
> <property name="tomcat.home" value="${basedir}"/>

Those basedir definitions definitely look weird to me.

> <target name="jspc" > <taskdef classname="org.apache.jasper.JspC"
> name="jasper2"  >

You could also do this to define all your tasks:

  <import file="${catalina.home}/bin/catalina-tasks.xml" />

> <classpath id="jspc.classpath"> <pathelement
> location="${java.home}/../lib/tools.jar"/>

You don't need tools.jar anymore. This must be really old.

> <jasper2 validateXml="false" uriroot="${webapp.path}" 
> webxml="${outputdir}/WEB-INF/web.xml" 
> webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml" 
> outputDir="${outputdir}/WEB-INF/src"> </jasper2>

Okay, does that work? Specifying webxml and webXMLFragment probably
isn't necessary.

> <java classname="org.apache.jasper.JspC" fork="true" 
> dir="${outputdir}/WEB-INF/src">

Invoking JspC again? That's just Jasper all over again. At this point,
all you need to do it compile the generated .java files using <javac>.

> Right now I have this build file called build.xml inside the build
> folder in myapp. So final structure is myapp has bunch of jsp files
> and a build folder with build.xml to precompile the jsps. When I
> run the above build.xml using ant I get the following error.
> 
> 
> BUILD FAILED 
> C:\apache-tomcat-7.0.16\webapps\myapp\build\build.xml:29:

You should upgrade to 7.0.22 if you can possibly do it.

I've been working on a build-jspc.xml ant script that should take care
of a number of these tasks for you. Here it is in it's current form.
No promises. Put the following file into catalina.home/build-jspc.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
- -->
<project name="Tomcat-JSPC" default="" basedir=".">
  <import file="${catalina.home}/bin/catalina-tasks.xml" />

  <target name="jspc" description="Compiles JSPs"
depends="jspc:jasper, jspc:javac, jspc:jar">
  </target>

  <target name="jspc:jasper">
    <condition property="jspc-configuration-okay">
      <and>
        <isset property="catalina.home" />
        <isset property="jspc.target.dir" />
        <isset property="jspc.source.dir" />
        <resourcecount refid="compile.classpath" when="greater"
count="0" />
      </and>
    </condition>

    <fail unless="jspc-configuration-okay">
      JspC requires the following properties to be set:

      catalina.home=${catalina.home}
      jspc.target.dir=${jspc.target.dir}
      jspc.source.dir=${jspc.source.dir}

      Also, you need to have the following classpath reference set:
      &lt;path id="jspc.classpath"&gt;

      It should point to all of the classes and libraries referenced
      by your JSP sources.
    </fail>

    <delete dir="${jspc.target.dir}" />
    <mkdir dir="${jspc.target.dir}" />
    <mkdir dir="${jspc.target.dir}/src" />
    <mkdir dir="${jspc.target.dir}/classes" />
    <mkdir dir="${jspc.target.dir}/META-INF" />

    <!-- Define Jasper task with preferred classpath -->
    <taskdef classname="org.apache.jasper.JspC" name="jasper">
      <classpath>
        <path refid="jspc.classpath" />
        <fileset dir="${catalina.home}/bin" includes="*.jar" />
        <fileset dir="${catalina.home}/lib" includes="*.jar" />
      </classpath>
    </taskdef>

    <!-- Invoke Jasper -->
    <jasper compile="false"
            uriroot="${jspc.source.dir}"
            outputDir="${jspc.target.dir}/src"
            webXmlFragment="${jspc.target.dir}/META-INF/web-fragment.xml">
    </jasper>
  </target>

  <target name="jspc:javac">
    <!-- Compile the classes -->
    <javac srcdir="${jspc.target.dir}/src"
           destdir="${jspc.target.dir}/classes"
           deprecation="${javac.deprecation}"
           includeAntRuntime="false">
      <classpath>
        <path refid="jspc.classpath" />
        <fileset dir="${catalina.home}/lib" includes="*.jar" />
      </classpath>
    </javac>
  </target>

  <target name="jspc:jar" if="jspc.jar.file">
    <!-- Package into a .jar file -->
    <jar
         destfile="${jspc.jar.file}"
         compress="true">
      <fileset dir="${jspc.target.dir}"
includes="META-INF/web-fragment.xml" />
      <fileset dir="${jspc.target.dir}/classes" includes="**/*" />
    </jar>
  </target>
</project>


Now, add a target like this to your own ant build file:

  <target name="jspc" depends="build">
    <property name="catalina.home" value="/path/to/tomcat7" />

    <!--
    <property name="jspc.target.dir" value="tmp" />

    <!-- contains your webapp (has a WEB-INF directory, etc.) -->
    <property name="jspc.source.dir" value="jspc" />
    <property name="jspc.jar.file" value="jsps.jar" />

    <path id="jspc.classpath">
      <path refid="compile.classpath" />
    </path>

    <ant antfile="${catalina.home}/build-jspc.xml"
         target="jspc"
         inheritRefs="true"
         />
  </target>

When you run "ant jspc" you should end up with a jsps.jar file in your
project's directory that contains your compiled .jsp files as well as
a META-INF/web-fragment.xml file that you can merge-into your web.xml
file.

I'm working on getting META-INF/web-fragment.xml to be
spec-3.0-compliant so you can just drop your .jar file into
WEB-INF/jsps.jar and let the container take care of everything.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk6LMiEACgkQ9CaO5/Lv0PDLmwCePR2X6J1/CDZy8DYmeZHJzlJJ
RtQAoL0TivtEerhipWRSr/LpaAAdXme6
=tT7z
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org