通過ant來打jar,war包

--北京。。

 今天在研究ant,下面先貼出用ant的junit來實現對代碼的編譯並測試

先貼出測試項目的結構目錄:

編譯之前需要把項目所使用的jar包放在lib裏面

1、使用ant實現junit  test測試

<?xml version="1.0"?>
<project name="testPoi" default="doc">

<!-- properies -->
<property name="src.dir" value="src" />
<property name="report.dir" value="report" />
<property name="classes.dir" value="classes" />
<property name="lib.dir" value="lib" />
<property name="dist.dir" value="dist" />
<property name="doc.dirA" value="doc"/>

<!-- 定義classpath -->
<path id="master-classpath">
<fileset file="${lib.dir}/*.jar" />
<pathelement path="${classes.dir}"/>
</path>

<!-- 初始化任務 -->
<target name="init">
</target>

<!-- 編譯 -->
<target name="compile" depends="init" description="compile the source files">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.6" includeantruntime="on">
<classpath refid="master-classpath"/>
</javac>
</target>

<!-- 測試 -->
<target name="test" depends="compile" description="run junit test">
<mkdir dir="${report.dir}"/>
<junit printsummary="on" haltonfailure="false"  failureproperty="tests.failed" showoutput="true">
<classpath refid="master-classpath" />
<formatter type="plain"/>
<batchtest todir="${report.dir}">
<fileset dir="${classes.dir}">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
<fail if="tests.failed">
***********************************************************
**** One or more tests failed! Check the output ... ****
***********************************************************
</fail>
</target>

<!-- 打包成jar -->
<target name="pack" depends="test" description="make .jar file">
<mkdir dir="${dist.dir}" />
<jar destfile="${dist.dir}/testPoi.jar" basedir="${classes.dir}">
<exclude name="**/*Test.*" />
<exclude name="**/Test*.*" />
</jar>
</target>

<!-- 輸出api文檔 -->
<target name="doc" depends="pack" description="create api doc">
<mkdir dir="${doc.dir}" />
<javadoc destdir="${doc.dir}"
author="true"
version="true"
use="true"
windowtitle="Test API">
<packageset dir="${src.dir}" defaultexcludes="yes">
<include name="example/**" />
</packageset>
<doctitle><![CDATA[<h1>Hello, test</h1>]]></doctitle>
<bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom>
<tag name="todo" scope="all" description="To do:" />
</javadoc>
</target>
</project>

2、ant將代碼打成jar

貼出build.xml的配置

<project name="textPoi" default="deploy-textPoi" basedir=".">

	<target name="deploy-textPoi" depends="init,clean,get-lib,copymapper,compile,jar">
	</target>

	<description>
		deploy-testPoi build.xml
    </description>
	<property file="${basedir}\build.properties" />
	<property name="lib" location="lib" />
	<property name="src" location="src" />
	<property name="bin" location="bin" />
	<property name="runjar" location="runjar" />
	<!--<property name="conf" location="config" />-->
	<property name="resource" location="resource" />
	<property name="Lib-FDAS" value="../testPoi/lib" />

	<!--<property name="FXpayServiceBin" value="../FXpayServiceBin" />-->

	<path id="classpath">
		<fileset dir="${lib}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<target name="init">
		<tstamp />
	</target>

	<target name="clean" description="clean up">
		<delete dir="${bin}" />
		<delete dir="${runjar}" />
		<mkdir dir="${bin}" />
		<mkdir dir="${runjar}" />
	</target>


	<target name="get-lib" description="get java library">
		<copy todir="${runjar}/lib">
			<fileset dir="${lib}">
				<include name="*.*" />
			</fileset>
		</copy>
	</target>

	<target name="copymapper" description="copy mapper文件">
		<mkdir dir="${bin}/${mapper.path}" />
		<copy todir="${bin}/${mapper.path}">
			<fileset dir="${src}/${mapper.path}">
				<include name="**/*.xml" />
			</fileset>
		</copy>
	</target>

	<target name="compile" description="compile the source ">
		<javac srcdir="${src}" destdir="${bin}" encoding="UTF-8" debug="true" executable="${JAVA_HOME}/bin/javac" includeantruntime="false" source="1.6" target="1.6">
			<classpath refid="classpath" />
			<compilerarg value="-Xlint:unchecked -Xlint:deprecation" />
			<exclude name="**/*.svn" />
		</javac>
	</target>



	<target name="jar" description="jar">
		<pathconvert property="mf.classpath" pathsep=" ">
			<mapper>
				<chainedmapper>
					<flattenmapper />
					<globmapper from="*.jar" to="./lib/*.jar" />
				</chainedmapper>
			</mapper>
			<path refid="classpath" />
		</pathconvert>
		<jar destfile="${runjar}/${build.jar}" basedir="${bin}">
			<!--<fileset dir="${conf}" includes="*.*" />-->
			<!--<fileset dir="${resource}" includes="*.*" />-->
			<manifest>
				<attribute name="Main-class" value="./*" />
				<attribute name="Class-Path" value="${mf.classpath}" />
			</manifest>
		</jar>
		
	</target>

	
</project>
中間涉及到一個配置文件build.properties

mapper.path=equals
model.path=model
build.jar=testPoi-1.0.0.jar
#build.common.jar=hbservice-common-1.0.0.jar
common-src=${mapper.path}/** ${model.path}/**

3、使用ant來實現打war包(打war包的主要是對web工程進行打包,讓war放在tomcat下直接能使用)

在生成war的過程中一直出現

這個原因待到快解決的時候終於想明白了,lib包裏面一定要把項目所需要的jar拷進去,在編譯的時候需要用到lib裏面的jar來進行jar包依賴。

因爲沒有這個jar,所以一直出現這問題。

因爲是web項目,現將項目的結構圖貼上


下面貼出完整的build.xml

<?xml version="1.0" ?> 
<project name="hbcall" default="war">

	<path id="compile.classpath">
		<fileset dir="WebRoot/WEB-INF/lib">
			<include name="*.jar"/>
		</fileset>
	</path>
	
	<target name="init">
		<mkdir dir="build/classes"/>
		<mkdir dir="dist" />
	</target>
	
	<!--<target name="compile" depends="init" >
		<javac destdir="build/classes" debug="true" srcdir="src">
			<classpath refid="compile.classpath"/>
		</javac>
	</target>-->
	<target name="copymapper" description="copy mapper文件">
			<mkdir dir="build/config" />
			<copy todir="build/config">
				<fileset dir="config">
					<include name="**/*.xml" />
				</fileset>
			</copy>
	</target>
	
	<target name="compile" depends="init" description="compile the source files">
	<javac srcdir="src" destdir="build/classes" target="1.6" includeantruntime="on">
	<classpath refid="compile.classpath"/>
	</javac>
	</target>
	
	<target name="war" depends="compile">
		<war destfile="dist/AntExample.war" webxml="WebRoot/WEB-INF/web.xml">
			<fileset dir="WebRoot"/>
			<lib dir="WebRoot/WEB-INF/lib"/>
			<classes dir="build/classes"/>
		</war>
	</target>
	
	<!--<target name="clean">
		<delete dir="dist" />
		<delete dir="build" />
	</target>-->
	
</project>

即能成功生成出war包,放在tomcat的webapps下面,重啓tomcat能夠正確的編譯出項目的整個編譯代碼。

美中不足的,build.xml沒有配置copy配置文件,需要手動將配置文件給copy到相應位置。

ok~整個ant基本情況就是這樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章