- 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" failοnerrοr="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" failοnerrοr="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
转载于:https://blog.51cto.com/lya041/1182365