Tomcat JSP預編譯

 

  1. 1. 編寫構建配置文件build.properties 
  2. tomcat.home=D:/Tomcat/apache-tomcat-7.0.14 
  3. java.home=D:/Program Files/Java/jdk1.6.0_25 
  4. webapp.name=Test 
  5. webapp.path=E/:/WorkSpace/MyEclipse9.0/Test/WebRoot 
  6. 2.編寫Ant構建腳本build.xml 
  7. 以下爲Tomcat6.X以上腳本 
  8. <?xml version="1.0" encoding="UTF-8"?> 
  9. <project name="WebApp Precompilation JSP to Java to Class to Jar" basedir="." default="help"
  10.     <property file="build.properties" /> 
  11.     <target name="all" depends="jsp2java,java2class,class2jar,clear" /> 
  12.     <target name="help"
  13.         <echo message="顯示功能列表" /> 
  14.         <echo message="jsp2java  通過JspC將JSP轉換成Java源代碼" /> 
  15.         <echo message="java2class 將轉換後的Java源代碼進行編譯成class文件" /> 
  16.         <echo message="class2jar 將編譯後的class文件打包" /> 
  17.         <echo message="clear  清理現場" /> 
  18.     </target> 
  19.     <target name="jsp2java"
  20.         <taskdef classname="org.apache.jasper.JspC" name="jsp2java"
  21.             <classpath id="jsp2java.classpath"
  22.                 <fileset dir="${tomcat.home}/bin"
  23.                     <include name="*.jar" /> 
  24.                 </fileset> 
  25.                 <fileset dir="${tomcat.home}/lib"
  26.                     <include name="*.jar" /> 
  27.                 </fileset> 
  28.             </classpath> 
  29.         </taskdef> 
  30.         <!-- 注意JSP文件要設置爲UTF-8編碼 --> 
  31.         <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" /> 
  32.     </target> 
  33.     <target name="java2class"
  34.         <mkdir dir="${webapp.path}/WEB-INF/JspC/classes" /> 
  35.         <!-- 同樣Java文件要設置爲UTF-8編碼 --> 
  36.         <javac srcdir="${webapp.path}/../src" destdir="${webapp.path}/WEB-INF/JspC/classes" encoding="UTF-8" optimize="off" debug="on" failonerror="false" excludes="**/*.smap"
  37.             <classpath id="java2class.classpath"
  38.                 <pathelement location="${webapp.path}/WEB-INF/classes" /> 
  39.                 <fileset dir="${webapp.path}/WEB-INF/lib"
  40.                     <include name="*.jar" /> 
  41.                 </fileset> 
  42.                 <fileset dir="${tomcat.home}/bin"
  43.                     <include name="*.jar" /> 
  44.                 </fileset> 
  45.             </classpath> 
  46.             <include name="**" /> 
  47.             <exclude name="tags/**" /> 
  48.         </javac> 
  49.     </target> 
  50.     <target name="class2jar"
  51.         <mkdir dir="${webapp.path}/WEB-INF/lib" /> 
  52.         <jar basedir="${webapp.path}/WEB-INF/JspC/classes" jarfile="${webapp.path}/WEB-INF/lib/${webapp.name}JSP.jar" /> 
  53.     </target> 
  54.     <target name="clear"
  55.         <delete dir="${webapp.path}/WEB-INF/JspC/classes" /> 
  56.         <delete dir="${webapp.path}/WEB-INF/JspC/src" /> 
  57.         <delete dir="${webapp.path}/WEB-INF/JspC" /> 
  58.     </target> 
  59. </project> 
  60. 以下爲Tomcat5.5以前版本構建腳本 
  61. <?xml version="1.0" encoding="UTF-8"?> 
  62. <project name="WebApp Precompilation JSP to Java to Class to Jar" basedir="." default="help"
  63.     <property file="build.properties" /> 
  64.     <target name="all" depends="jsp2java,java2class,class2jar,clear" /> 
  65.     <target name="help"
  66.         <echo message="顯示功能列表" /> 
  67.         <echo message="jsp2java  通過JspC將JSP轉換成Java源代碼" /> 
  68.         <echo message="java2class 將轉換後的Java源代碼進行編譯成class文件" /> 
  69.         <echo message="class2jar 將編譯後的class文件打包" /> 
  70.         <echo message="clear  清理現場" /> 
  71.     </target> 
  72.     <target name="jsp2java"
  73.         <taskdef classname="org.apache.jasper.JspC" name="jsp2java"
  74.             <classpath id="jsp2java.classpath"
  75.                 <fileset dir="${tomcat.home}/bin"
  76.                     <include name="*.jar" /> 
  77.                 </fileset> 
  78.                 <fileset dir="${tomcat.home}/server/lib"
  79.                     <include name="*.jar" /> 
  80.                 </fileset> 
  81.                 <fileset dir="${tomcat.home}/common/lib"
  82.                     <include name="*.jar" /> 
  83.                 </fileset> 
  84.             </classpath> 
  85.         </taskdef> 
  86.         <!-- 注意JSP文件要設置爲UTF-8編碼 --> 
  87.         <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" /> 
  88.     </target> 
  89.     <target name="java2class"
  90.         <mkdir dir="${webapp.path}/WEB-INF/JspC/classes" /> 
  91.         <!-- 同樣Java文件要設置爲UTF-8編碼 --> 
  92.         <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"
  93.             <classpath id="java2class.classpath"
  94.                 <pathelement location="${webapp.path}/WEB-INF/classes" /> 
  95.                 <fileset dir="${webapp.path}/WEB-INF/lib"
  96.                     <include name="*.jar" /> 
  97.                 </fileset> 
  98.                 <pathelement location="${tomcat.home}/common/classes" /> 
  99.                 <fileset dir="${tomcat.home}/common/lib"
  100.                     <include name="*.jar" /> 
  101.                 </fileset> 
  102.                 <pathelement location="${tomcat.home}/shared/classes" /> 
  103.                 <fileset dir="${tomcat.home}/shared/lib"
  104.                     <include name="*.jar" /> 
  105.                 </fileset> 
  106.                 <fileset dir="${tomcat.home}/bin"
  107.                     <include name="*.jar" /> 
  108.                 </fileset> 
  109.             </classpath> 
  110.             <include name="**" /> 
  111.             <exclude name="tags/**" /> 
  112.         </javac> 
  113.     </target> 
  114.     <target name="class2jar"
  115.         <mkdir dir="${webapp.path}/WEB-INF/lib" /> 
  116.         <jar basedir="${webapp.path}/WEB-INF/JspC/classes" jarfile="${webapp.path}/WEB-INF/lib/${webapp.name}JSP.jar" /> 
  117.     </target> 
  118.     <target name="clear"
  119.         <delete dir="${webapp.path}/WEB-INF/JspC/classes" /> 
  120.         <delete dir="${webapp.path}/WEB-INF/JspC/src" /> 
  121.         <delete dir="${webapp.path}/WEB-INF/JspC" /> 
  122.     </target> 
  123. </project> 
  124. 3. 設置好Ant的path環境變量, 
  125. 4. 修改build.properties, 
  126. 5. 執行ant all命令即可。 
  127. 6. 生成好的jar文件是{$webappname}JSP.jar。 
  128. 7. 在做爲產品發佈的時候,將依賴類包和類jar包和JSP預編譯的包放到WEB-INF/lib/目錄下即可; 
  129. 8. 然後刪除掉你的所有的預編過的JSP源文件; 
  130. 9. 將${webapp.path}/WEB-INF/webJSP.xml裏的servlet映射,添加到${webapp.path}/WEB-INF/web.xml中。 
  131. 說明:建設使用MyEclipse Ant構建,生成時注意工程JDK版本與JAVE_HOME JDK版本保持一致 
  132. 更多資源參考: 
  133. http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html 
  134. http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/jasper/JspC.html 
  135. http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/jasper/JspC.html 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章