- 1. 編寫構建配置文件build.properties
- tomcat.home=D:/Tomcat/apache-tomcat-7.0.14
- java.home=D:/Program Files/Java/jdk1.6.0_25
- webapp.name=Test
- webapp.path=E/:/WorkSpace/MyEclipse9.0/Test/WebRoot
- 2.編寫Ant構建腳本build.xml
- 以下爲Tomcat6.X以上腳本
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="WebApp Precompilation JSP to Java to Class to Jar" basedir="." default="help">
- <property file="build.properties" />
- <target name="all" depends="jsp2java,java2class,class2jar,clear" />
- <target name="help">
- <echo message="顯示功能列表" />
- <echo message="jsp2java 通過JspC將JSP轉換成Java源代碼" />
- <echo message="java2class 將轉換後的Java源代碼進行編譯成class文件" />
- <echo message="class2jar 將編譯後的class文件打包" />
- <echo message="clear 清理現場" />
- </target>
- <target name="jsp2java">
- <taskdef classname="org.apache.jasper.JspC" name="jsp2java">
- <classpath id="jsp2java.classpath">
- <fileset dir="${tomcat.home}/bin">
- <include name="*.jar" />
- </fileset>
- <fileset dir="${tomcat.home}/lib">
- <include name="*.jar" />
- </fileset>
- </classpath>
- </taskdef>
- <!-- 注意JSP文件要設置爲UTF-8編碼 -->
- <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" />
- </target>
- <target name="java2class">
- <mkdir dir="${webapp.path}/WEB-INF/JspC/classes" />
- <!-- 同樣Java文件要設置爲UTF-8編碼 -->
- <javac srcdir="${webapp.path}/../src" destdir="${webapp.path}/WEB-INF/JspC/classes" encoding="UTF-8" optimize="off" debug="on" failonerror="false" excludes="**/*.smap">
- <classpath id="java2class.classpath">
- <pathelement location="${webapp.path}/WEB-INF/classes" />
- <fileset dir="${webapp.path}/WEB-INF/lib">
- <include name="*.jar" />
- </fileset>
- <fileset dir="${tomcat.home}/bin">
- <include name="*.jar" />
- </fileset>
- </classpath>
- <include name="**" />
- <exclude name="tags/**" />
- </javac>
- </target>
- <target name="class2jar">
- <mkdir dir="${webapp.path}/WEB-INF/lib" />
- <jar basedir="${webapp.path}/WEB-INF/JspC/classes" jarfile="${webapp.path}/WEB-INF/lib/${webapp.name}JSP.jar" />
- </target>
- <target name="clear">
- <delete dir="${webapp.path}/WEB-INF/JspC/classes" />
- <delete dir="${webapp.path}/WEB-INF/JspC/src" />
- <delete dir="${webapp.path}/WEB-INF/JspC" />
- </target>
- </project>
- 以下爲Tomcat5.5以前版本構建腳本
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="WebApp Precompilation JSP to Java to Class to Jar" basedir="." default="help">
- <property file="build.properties" />
- <target name="all" depends="jsp2java,java2class,class2jar,clear" />
- <target name="help">
- <echo message="顯示功能列表" />
- <echo message="jsp2java 通過JspC將JSP轉換成Java源代碼" />
- <echo message="java2class 將轉換後的Java源代碼進行編譯成class文件" />
- <echo message="class2jar 將編譯後的class文件打包" />
- <echo message="clear 清理現場" />
- </target>
- <target name="jsp2java">
- <taskdef classname="org.apache.jasper.JspC" name="jsp2java">
- <classpath id="jsp2java.classpath">
- <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>
- <!-- 注意JSP文件要設置爲UTF-8編碼 -->
- <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" />
- </target>
- <target name="java2class">
- <mkdir dir="${webapp.path}/WEB-INF/JspC/classes" />
- <!-- 同樣Java文件要設置爲UTF-8編碼 -->
- <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">
- <classpath id="java2class.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="class2jar">
- <mkdir dir="${webapp.path}/WEB-INF/lib" />
- <jar basedir="${webapp.path}/WEB-INF/JspC/classes" jarfile="${webapp.path}/WEB-INF/lib/${webapp.name}JSP.jar" />
- </target>
- <target name="clear">
- <delete dir="${webapp.path}/WEB-INF/JspC/classes" />
- <delete dir="${webapp.path}/WEB-INF/JspC/src" />
- <delete dir="${webapp.path}/WEB-INF/JspC" />
- </target>
- </project>
- 3. 設置好Ant的path環境變量,
- 4. 修改build.properties,
- 5. 執行ant all命令即可。
- 6. 生成好的jar文件是{$webappname}JSP.jar。
- 7. 在做爲產品發佈的時候,將依賴類包和類jar包和JSP預編譯的包放到WEB-INF/lib/目錄下即可;
- 8. 然後刪除掉你的所有的預編過的JSP源文件;
- 9. 將${webapp.path}/WEB-INF/webJSP.xml裏的servlet映射,添加到${webapp.path}/WEB-INF/web.xml中。
- 說明:建設使用MyEclipse Ant構建,生成時注意工程JDK版本與JAVE_HOME JDK版本保持一致
- 更多資源參考:
- http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html
- http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/jasper/JspC.html
- http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/jasper/JspC.html