Tomcat 预编译JSP 脚本

参考:

The Apache Jakarta Tomcat 5.5 Servlet/JSP Container Jasper 2 JSP Engine How To

http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jasper-howto.html

jspc

http://ant.apache.org/manual/OptionalTasks/jspc.html

用Tomcat进行预编译的ant脚本如下:

build.properties的内容为:

tomcat.home=D:/Tomcat 5.5
webapp.name=blh
webapp.path=D:/Tomcat 5.5/webapps/blh

build.xml的内容为:

  1. <?xml version="1.0" encoding="GBK"?>
  2. <project name="WebApp Precompilation JSP to Java to Class to Jar" basedir="." default="help">
  3.  <property file="build.properties"/>
  4.  <target name="all" depends="jsp2java,java2class,class2jar,clear"/>
  5.  <target name="help">
  6.   <echo message="显示功能列表"/>
  7.   <echo message="jsp2java  通过JspC将JSP转换成Java源代码"/>
  8.   <echo message="java2class 将转换后的Java源代码进行编译成class文件"/>
  9.   <echo message="class2jar 将编译后的class文件打包"/>
  10.   <echo message="clear  清理现场"/>
  11.  </target>
  12.  <target name="jsp2java">
  13.   <taskdef classname="org.apache.jasper.JspC" name="jsp2java">
  14.    <classpath id="jsp2java.classpath">
  15.     <fileset dir="${tomcat.home}/bin">
  16.      <include name="*.jar"/>
  17.     </fileset>
  18.     <fileset dir="${tomcat.home}/server/lib">
  19.      <include name="*.jar"/>
  20.     </fileset>
  21.     <fileset dir="${tomcat.home}/common/lib">
  22.      <include name="*.jar"/>
  23.     </fileset>
  24.    </classpath>
  25.   </taskdef>
  26.   <!-- 注意JSP文件要设置为UTF-8编码 -->
  27.   <jsp2java classpath="jsp2java.classpath" javaEncoding="UTF-8" validateXml="false" uriroot="${webapp.path}" webXmlFragment="${webapp.path}/WEB-INF/webJSP.xml" outputDir="${webapp.path}/WEB-INF/JspC/src"/>
  28.  </target>
  29.  <target name="java2class">
  30.   <mkdir dir="${webapp.path}/WEB-INF/JspC/classes"/>
  31.   <!-- 同样Java文件要设置为UTF-8编码 -->
  32.   <javac srcdir="${webapp.path}/WEB-INF/JspC/src" destdir="${webapp.path}/WEB-INF/JspC/classes" encoding="UTF-8" optimize="off" debug="on" failonerror="false" excludes="**/*.smap">
  33.    <classpath id="java2class.classpath">
  34.     <pathelement location="${webapp.path}/WEB-INF/classes"/>
  35.     <fileset dir="${webapp.path}/WEB-INF/lib">
  36.      <include name="*.jar"/>
  37.     </fileset>
  38.     <pathelement location="${tomcat.home}/common/classes"/>
  39.     <fileset dir="${tomcat.home}/common/lib">
  40.      <include name="*.jar"/>
  41.     </fileset>
  42.     <pathelement location="${tomcat.home}/shared/classes"/>
  43.     <fileset dir="${tomcat.home}/shared/lib">
  44.      <include name="*.jar"/>
  45.     </fileset>
  46.     <fileset dir="${tomcat.home}/bin">
  47.      <include name="*.jar"/>
  48.     </fileset>
  49.    </classpath>
  50.    <include name="**"/>
  51.    <exclude name="tags/**" />
  52.   </javac>
  53.  </target>
  54.  <target name="class2jar">
  55.   <mkdir dir="${webapp.path}/WEB-INF/lib"/>
  56.   <jar basedir="${webapp.path}/WEB-INF/JspC/classes" jarfile="${webapp.path}/WEB-INF/lib/${webapp.name}JSP.jar"/>
  57.  </target>
  58.  <target name="clear">
  59.   <delete dir="${webapp.path}/WEB-INF/JspC/classes"/>
  60.   <delete dir="${webapp.path}/WEB-INF/JspC/src"/>
  61.   <delete dir="${webapp.path}/WEB-INF/JspC"/>
  62.  </target>
  63. </project>

只需要设置好Ant的path环境变量,然后修改build.properties。执行ant all命令即可。
生成好的jar文件是{$webappname}JSP.jar。
在做为产品发布的时候,只需要你的类jar包和JSP预编译的包放到WEB-INF/lib/目录下即可,如${webappname}.jar和JSP预编译的包${webappname}JSP.jar;
然后删除掉你的所有的预编过的JSP源文件;
并且${webapp.path}/WEB-INF/webJSP.xml里的servlet映射,添加到${webapp.path}/WEB-INF/web.xml中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值