Using Ant is the preferred way to compile web applications using JSPC.
Use the script given below (a similar script is included in the "deployer"
download) to precompile a webapp:
| | |
|
<project name="Webapp Precompilation" default="all" basedir=".">
<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="compile">
<mkdir dir="${webapp.path}/WEB-INF/classes"/>
<mkdir dir="${webapp.path}/WEB-INF/lib"/>
<javac destdir="${webapp.path}/WEB-INF/classes"
optimize="off"
debug="on" failonerror="false"
srcdir="${webapp.path}/WEB-INF/src"
excludes="**/*.smap">
<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="all" depends="jspc,compile">
</target>
<target name="cleanup">
<delete>
<fileset dir="${webapp.path}/WEB-INF/src"/>
<fileset dir="${webapp.path}/WEB-INF/classes/org/apache/jsp"/>
</delete>
</target>
</project>
| |
| | |
下面的命令行可以用来运行这个script(用Tomcat基础路径和指向应该被预编译的webapp的路径来代替那些记号)。
| | |
|
$ANT_HOME/bin/ant -Dtomcat.home=<$TOMCAT_HOME> -Dwebapp.path=<$WEBAPP_PATH>
| |
| | |
然后,对于在预编译过程中产生的servlets的声明和映射必须被加入到网络程序部署描述符里去。把${webapp.path}/WEB-INF/generated_web.xml插入到${webapp.path}/WEB-INF/web.xml文件应该被放置的地方。重新启动web程序(使用the manager),并测试它以验证预编译过的servlets能正常运行。网络程序部署描述符里适当的标记(token)也可以被用来通过Ant过滤功能自动插入所产生servlets声明和映射。事实上这就是Tomcat发布的所有webapps作为build过程的一部分是怎样自动被编译的。
At the jasper2 task you can use the option addWebXmlMappings
for
automatic merge the ${webapp.path}/WEB-INF/generated_web.xml
with the current web application deployment descriptor at ${webapp.path}/WEB-INF/web.xml
.
When you want to use Java 5 feature inside your jsp's, add the following javac compiler task
attributes: source="1.5" target="1.5"
. For live application
you can also compile with optimize="on"
and without debug info
debug="off"
.
When you don't want to stop the jsp generation at first jsp syntax error, use
failOnError="false"
and with showSuccess="true"
all successfull jsp to java generation are printed out. Sometimes it is
very helpfull, when you cleanup the generate java source files at ${webapp.path}/WEB-INF/src
and the compile jsp servlet classes at ${webapp.path}/WEB-INF/classes/org/apache/jsp
.
Hints:
- When you switch to another tomcat release, then regenerate and recompile
your jsp's with this version again!
- Use java system property at server runtime to disable tag pooling
org.apache.jasper.runtime.JspFactoryImpl.USE_POOL=false
.
and limit the buffering with org.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true
. Note that changing
from the defaults may affect performance, but depending on the application.