Ant配置文件build.xml示例(SVN、Junit、Mail)

网上关于Ant原理介绍的文章特别多,也有不少朋友提供build.xml配置文件应该如何写,但很少有配置文件能够拿来即用的。本文并不打算介绍如何使用ant,只是想贴出一个能够成功运行的build.xml的示例,并说明如何通过ant进行svn、junit、mail的操作,以作备忘。


Ant集成SVN

ant集成svn,需要提供svn需要的jar包,SVNANT项目(http://subclipse.tigris.org/svnant.html)对其提供了支持。下载最新的SVNANT,将lib下面的svnant.jar、svnClientAdapter.jar、svnjavahl.jar和svnkit.jar放入ant工程的lib目录中。然后就可以在build.xml中使用svn了。


在build.xml中使用SVN也非常简单,仅需两步:

1. 添加如下语句到build.xml中。

<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="compile-path" />

2. 在target中使用svn。

<target name="checkout">
	<svn username="lili" password="lili">
		<checkout url="http://10.10.72.203/svn/project" destPath="${tmp.dir}" revision="HEAD"/> 
	</svn> 
</target>


一般项目存在src目录和test目录,对这两个目录分别进行checkout会出现 svn is already a working copy for a different URL  错误,大概是由于在同一个文件夹下checkout两个不同url的目录,会受到".svn"的干扰,一个比较sb的解决办法就是讲项目全部checkout出来,然后手动将src和test目录中的文件复制到相应的目录,如后面的build.xml示例所示。


Ant集成Junit

ant集成Junit非常简单,只需将junit.jar放到ant工程的lib目录下即可。只是需要注意,运行junit时需要指定TEST类编译后的classes文件,这部分文件一般是前面compile任务所产生的,配置该路径即可。


具体使用方法如后面的build.xml示例所示。


Ant集成Mail
ant集成mail需要两个jar包,分别是:mail.jar、activation.jar。由于ant 中的mail命令并没有提供像javac那样类似的classpath属性,也就是说无法为mail命令指定执行时候的CLASSPATH,于是就不能把这两个jar包放到ant工程的lib下,即使放到ant工程的lib目录下,mail命令同样找不到对应的jar包,解决办法是将这两个jar包放入ANT_HOME的lib目录下。因为ANT_HOME/lib下面的jar包会自动加载的。


特别注意,如果你使用eclipse运行ant命令,需要在eclipsse中指定ANT_HOME,不然eclipse使用自带的ant,这是同样会出现找不到jar包的问题。修改eclipse中ANT_HOME的方式如下:Window->Preferences, Ant->Runtime, 点击右侧的Ant home, 关联正确的ANT_HOME即可。


关于mail部分的配置如后面的build.xml示例所示。


build.xml示例

<?xml version="1.0"?>
<project default="run-test">
	
	<property name="tmp.dir" location="tmp"></property>
	<property name="src.dir" location="src"></property>
	<property name="test.dir" location="test"></property>
	<property name="lib.dir" location="lib"></property>
	<property name="build.dir" location="build"></property>
	<property name="build.classes" location="${build.dir}/classes"></property>
	<property name="build.report" location="${build.dir}/report"></property>
	
	<path id="compile-path">
		<fileset dir="${lib.dir}" includes="**/*.jar"></fileset>   

	</path>
	
	<path id="test-compile-path">
		<path refid="compile-path"></path>
		<pathelement location="${build.classes}"/>
	</path>
	
	<target name="clean">
		<delete dir="${src.dir}"></delete>
		<delete dir="${test.dir}"></delete>
		<delete dir="${build.dir}"></delete>
	</target>
	
	<target name="init" depends="clean">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${build.classes}"/>
		<mkdir dir="${build.report}"/>
	</target>
	
	<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="compile-path" />
	
	<target name="checkout" depends="init">
		<mkdir dir="${tmp.dir}"/>
		
		<svn username="lili" password="lili">
			<checkout url="http://10.10.72.203/svn/project" destPath="${tmp.dir}" revision="HEAD"/>    
		</svn>
		
		<copy todir="${src.dir}">
			<fileset dir="${tmp.dir}/src" includes="**/*.java" />
		</copy>
		
		<copy todir="${test.dir}">
			<fileset dir="${tmp.dir}/test" includes="**/*.java" />
		</copy>
		
		<!--
		<copy todir="${lib.dir}">
			<fileset dir="${tmp.dir}/lib" includes="**/*.jar" />
		</copy>
		-->
		
		<delete dir="${tmp.dir}"/>
	</target>

	<target name="compile" depends="init,checkout">
		<javac srcdir="${src.dir}" destdir="${build.classes}" classpathref="compile-path"></javac>
	
		<copy todir="${build.classes}">
			<fileset dir="${src.dir}" excludes="**/*.java"></fileset>
		</copy>
			
	</target>
	
	<target name="test-compile" depends="compile">
		<javac srcdir="${test.dir}" destdir="${build.classes}" classpathref="test-compile-path"></javac>
	</target>
	
	<target name="run-test" depends="test-compile">
		<junit>
			<classpath refid="test-compile-path"></classpath>
			<formatter type="xml" usefile="true"/>
			
			<batchtest todir="${build.report}">
				<fileset dir="${build.classes}" includes="**/*Test.*"></fileset>
			</batchtest>
			
		</junit>
		
		<junitreport todir="${build.report}">
			<fileset dir="${build.report}" includes="TEST-*.xml"></fileset>
			<report format="frames" todir="${build.report}/html"/>
		</junitreport>
	</target>

	<target name="mail">
		<mail mailhost="smtp.163.com" mailport="25" user="test" password="test">
			<from address="test@163.com"/>
			<to address="563875233@qq.com"/>
			<message>This email is auto generated by ant</message>
			<fileset dir="${build.report}">
				<include name="**/*.*"/>
			</fileset>
		</mail>
	</target>
	
</project>








评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值