1. 程式人生 > >JSP編譯成servlet,並打包為jar

JSP編譯成servlet,並打包為jar

build.xml
<?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}/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.rootpath}/build/classes" />
				<fileset dir="${webapp.path}/WEB-INF/lib">
					<include name="*.jar" />
				</fileset>
				<fileset dir="${tomcat.home}/bin">
					<include name="*.jar" />
				</fileset>
				<fileset dir="${tomcat.home}/bin">
					<include name="*.jar" />
				</fileset>
				<fileset dir="${tomcat.home}/lib">
					<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>
build.properties
tomcat.home=D:/Tomcat6

java.home=C:/Java/jdk1.6.0_38

webapp.name=test

webapp.path=D:/projects/test/WebRoot

webapp.rootpath=D:/projects/test


參考資料:http://huiy.iteye.com/blog/2075541