通过ant来打jar,war包

时间:2021-03-05 09:12:45

--北京。。

 今天在研究ant,下面先贴出用ant的junit来实现对代码的编译并测试

先贴出测试项目的结构目录:

通过ant来打jar,war包

编译之前需要把项目所使用的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的过程中一直出现通过ant来打jar,war包通过ant来打jar,war包

这个原因待到快解决的时候终于想明白了,lib包里面一定要把项目所需要的jar拷进去,在编译的时候需要用到lib里面的jar来进行jar包依赖。

因为没有这个jar,所以一直出现这问题。

因为是web项目,现将项目的结构图贴上

通过ant来打jar,war包

下面贴出完整的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基本情况就是这样。