【0】README
1)本文旨在 给出 利用ant构建 jsp->servlet->class->jar 的分析;
2)本文部分内容转自:http://zfsn.iteye.com/blog/757919
【1】ant脚本内容 及其分析
1)build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="webNews" basedir="." default="servlet2class">
<property file="build.properties" />
<target name="all" depends="jsp2servlet,servlet2class,class2jar" />
<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="jsp2servlet">
<taskdef classname="org.apache.jasper.JspC" name="jsp2java">
<classpath id="jsp2servlet.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"
webXml="${webapp.path}/WebRoot/WEB-INF/web.xml"
outputDir="${webapp.path}/WebRoot/WEB-INF/JspC/src" />
</target>
<target name="servlet2class">
<mkdir dir="${webapp.path}/WebRoot/WEB-INF/JspC/classes" />
<javac
srcdir="${webapp.path}/WebRoot/WEB-INF/JspC/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" />
</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" /> -->
<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>
2)build.properties
#tomcat home
tomcat.home=D:\\Development\\Tomcat\\apache-tomcat-8.0.36
webapp.path=E:\\bench-cluster\\spring_in_action_eclipse\\precompileJSP
webapp.name=precompileJSP
对以上ant 脚本的分析(Analysis)
A1)jsp->servlet:(将jsp 转换为 servlet——特殊的java类)org.apache.jasper.JspC 的属性设置,参见 https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/jasper/JspC.html;<target name="jsp2servlet"> <taskdef classname="org.apache.jasper.JspC" name="jsp2java"> <classpath id="jsp2servlet.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" webXml="${webapp.path}/WebRoot/WEB-INF/web.xml" outputDir="${webapp.path}/WebRoot/WEB-INF/JspC/src" /> </target>
Attention)注意上述目录生成的 web.xml, 该文件设置了 servlet 到 uri 的 映射<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <!-- Automatically created by Apache Tomcat JspC. --> <web-app> <servlet> <servlet-name>org.apache.jsp.home_jsp</servlet-name> <servlet-class>org.apache.jsp.home_jsp</servlet-class> </servlet> <servlet> <servlet-name>org.apache.jsp.views.home_jsp</servlet-name> <servlet-class>org.apache.jsp.views.home_jsp</servlet-class> </servlet> <servlet-mapping> <servlet-name>org.apache.jsp.home_jsp</servlet-name> <url-pattern>/home.jsp</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>org.apache.jsp.views.home_jsp</servlet-name> <url-pattern>/views/home.jsp</url-pattern> </servlet-mapping> </web-app>
A2)servlet->class:(将 servlet 文件编译为 class文件)<target name="servlet2class"> <mkdir dir="${webapp.path}/WebRoot/WEB-INF/JspC/classes" /> <javac srcdir="${webapp.path}/WebRoot/WEB-INF/JspC/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" /> </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>
A3)class->jar:(将 上述编译得到的 class 文件打包为 jar)<target name="class2jar"> <!-- <mkdir dir="${webapp.path}/WebRoot/WEB-INF/lib" /> --> <jar jarfile="${webapp.path}/WebRoot/WEB-INF/lib/${webapp.name}JSP.jar" basedir="${webapp.path}/Webroot/WEB-INF/JspC/classes" /> </target>
A4)清理临时文件工作<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>
【2】windows 下执行上述命令的 console info
E:\bench-cluster\spring_in_action_eclipse\precompileJSP>ant jsp2servlet
Buildfile: E:\bench-cluster\spring_in_action_eclipse\precompileJSP\build.xml
jsp2servlet:
[jsp2java] 七月 08, 2016 2:31:24 下午 org.apache.jasper.servlet.TldScanner scanJars
[jsp2java] 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that w
ere scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
BUILD SUCCESSFUL
Total time: 0 seconds
E:\bench-cluster\spring_in_action_eclipse\precompileJSP>ant servlet2class
Buildfile: E:\bench-cluster\spring_in_action_eclipse\precompileJSP\build.xml
servlet2class:
[mkdir] Created dir: E:\bench-cluster\spring_in_action_eclipse\precompileJSP\WebRoot\WEB-INF\JspC\classes
[javac] E:\bench-cluster\spring_in_action_eclipse\precompileJSP\build.xml:45: warning: 'includeantruntime' was not set, defaulting to build.syscla
sspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to E:\bench-cluster\spring_in_action_eclipse\precompileJSP\Webroot\WEB-INF\JspC\classes
BUILD SUCCESSFUL
Total time: 0 seconds
E:\bench-cluster\spring_in_action_eclipse\precompileJSP>ant class2jar
Buildfile: E:\bench-cluster\spring_in_action_eclipse\precompileJSP\build.xml
class2jar:
[jar] Building jar: E:\bench-cluster\spring_in_action_eclipse\precompileJSP\WebRoot\WEB-INF\lib\precompileJSPJSP.jar
BUILD SUCCESSFUL
Total time: 0 seconds