<?xml version="1.0" encoding="GBK"?> <project name="webNews" basedir="." default="java2class"> <property file="build.properties"/> <target name="all" depends="jsp2java,java2class,class2jar"></target> <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> <jsp2java classpath="jsp2java.classpath" javaEncoding="UTF-8" validateXml="false" uriroot="${webapp.path}/WebRoot" webXmlFragment="${webapp.path}/WebRoot/WEB-INF/webJSP.xml" outputDir="${webapp.path}/WebRoot/WEB-INF/JspC/src"/> </target> <target name="java2class"> <mkdir dir="${webapp.path}/WebRoot/WEB-INF/JspC/classes"/> <javac srcdir="${webapp.path}/src" destdir="${webapp.path}/Webroot/WEB-INF/JspC/classes" encoding="utf-8" optimize="off" debug="on" failοnerrοr="false" excludes="**/*.smap"> <classpath id="java2class.classpath"> <fileset dir="${webapp.path}/WebRoot/WEB-INF/lib"> <include name="*.jar"></include> </fileset> <fileset dir="${tomcat.home}/lib"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/bin"> <include name="*.jar"/> </fileset> <pathelement location="${webapp.path}/WebRoot/WEB-INF/classes"/> </classpath> </javac> </target> <target name="class2jar"> <mkdir dir="${webapp.path}/WebRoot/WEB-INF/lib"></mkdir> <jar jarfile="${webapp.path}/WebRoot/WEB-INF/lib/${webapp.name}JSP.jar" basedir="${webapp.path}/Webroot/WEB-INF/JspC/classes"/> </target> <target name="clear"> <delete dir="${webapp.path}/WebRoot/WEB-INF/JspC/src"/> <delete dir="${webapp.path}/Webroot/WEB-INF/JspC/classes"/> <delete dir="${webapp.path}/WebRoot/WEB-INF/lib/${webapp.name}JSP.jar"></delete> </target> </project>
tomcat.home=C:/apache-tomcat-6.0.20
webapp.path=D:/workspace/demo
webapp.name=demo
只需要设置好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中。