利用ant构建 jsp->servlet->class->jar

【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类)
<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>
org.apache.jasper.JspC 的属性设置,参见   https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/jasper/JspC.html


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


<form class="ant-form ant-form-horizontal"><div class="ant-row ant-form-item"style="row-gap: 0px;"><div class="ant-col ant-form-item-label"style="width: 100px;"><label for="form_item_licDetailType"class="ant-form-item-required"title="license类型">license类型<!----></label></div><div class="ant-col ant-form-item-control"><div class="ant-form-item-control-input"><div class="ant-form-item-control-input-content"><div class="ant-select ant-select-single ant-select-allow-clear ant-select-show-arrow"><!----><div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search"id="form_item_licDetailType"autocomplete="off"class="ant-select-selection-search-input"role="combobox"aria-haspopup="listbox"aria-owns="form_item_licDetailType_list"aria-autocomplete="list"aria-controls="form_item_licDetailType_list"aria-activedescendant="form_item_licDetailType_list_0"readonly=""unselectable="on"style="opacity: 0;"aria-expanded="false"></span><!----><span class="ant-select-selection-placeholder">请选择</span></div><span class="ant-select-arrow"unselectable="on"aria-hidden="true"style="user-select: none;"><span role="img"aria-label="down"class="anticon anticon-down ant-select-suffix"><svg focusable="false"class=""data-icon="down"width="1em"height="1em"fill="currentColor"aria-hidden="true"viewBox="64 64 896 896"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span><!----></div></div><!----></div><!----><!----></div></div></form> 请进行selemiu 元素定位
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值