ANT 简单使用

公司有很多应用,刚开始开发的时候,都是把其他应用的build.xml复制过来,然后自己小改下就ok了,心里一点都不踏实,没有系统仔细的学习下,现写了两个小例子,应该还算比较全的,算巩固下ANT的知识点。

如果要打jar包的话,一般情况下,将ant编译后的文件放在一个目录例如bin目录下,应用的jar包放在ant编译的lib目录下,把bin目录下的文件全部打成一个jar包,lib下的jar包直接放在要使用ant打的jar包的应用中即可。

 

目录结构图如下:

 

例子1:

build.xml:

 

 

<?xml version="1.0" encoding="GBK"?>

<project name="TestAnt" default="build" basedir=".">
	
	<!--1、设置基本属性,设置基本路径,与eclipse的编译路径classes,bin无关-->
	<property name="src" value="src" />
	<property name="lib" value="lib" />
	
	<!--2、设置其他应用的路径(编译依赖工程)-->
	<property name="otherProject" value="../otherProject" ></property>
	
	<!--3、使用ant编译后存放编译文件的路径-->
	<property name="ooo" value="ooo" />

	<!--4、使用ant编译依赖的编译环境-->
	<path id="classpath">
		<pathelement location="bin" />
		<fileset dir="${lib}" includes="*.jar" />
	</path>
	
	<!--5、编译提示 -->
	<target name="usage">
		<property name="Name" value="TestAnt"  ></property>
		<property name="date" value="2011-08-11 22:09:30"  ></property>
		<echo message="----------------------- ${Name} [${date}] -----------------------" />
	</target>
	
	<!--6、创建使用ant编译存放编译文件的路径,先清除路径-->
	<target name="clean">
			<delete dir="${ooo}"></delete>
	</target>
	<target name="init">
		<mkdir dir="${ooo}" />
		<mkdir dir="${ooo}/lib"/>
	</target>
	
	<!--7、使用ant编译,并将编译的class文件放在 ${ooo}目录下 -->
	<target name="compile">
		<javac srcdir="${src}" destdir="${ooo}" debug="true" includes="**" >
				<classpath refid="classpath" />
		</javac>
	</target>
	
	<!--8、将scr下的非java类复制到${ooo}路径下(注意路径的一致性!)-->	
	<target name="copy">
		<copy todir="${ooo}">
			<fileset dir="${src}">
				<include name="**"/>
				<exclude name="**.properties" />
				<exclude name="**/*.properties" />
			</fileset>
		</copy>
		 <copy todir="${ooo}/lib" >
		 	<fileset dir="${lib}" >
		 		<include name="*.jar" />
		 	</fileset>
		 </copy>
	</target>
	<!--创建文件-->
<touch file="${classes.dir}/log/settle.log" ></touch>

<!--打包-->
<target name="init">
<jar destfile="${lib.dir}/stl-core.jar">
	<fileset dir="${frametemp}">
		<include name="**" />
	</fileset>
</jar>
</target>

<!-- 打包 -->
<target name="pack" depends="compile" description="make .jar file">
		<mkdir dir="${dist.dir}" />
		<jar destfile="${dist.dir}/stl-core.jar" basedir="${classes.dir}">
		</jar>
</target>
	<!--9、将ant编译的文件(${ooo}下的文件)打成jar包,可以有选择路径-->
	<target name="build.jar">
		<jar jarfile="${ooo}/xxx.jar" basedir="${ooo}" includes="**" />
	</target>
	
	<!--ant 编译执行 build 由 default="build" 决定 -->
	<!--执行build 依赖 usage,clean,init,compile,copy,build.jar,按顺序执行-->
	<target name="build" depends="usage,clean,init,compile,copy,build.jar" >
	</target>
</project>

 

 

例子2:

 

 

<?xml version="1.0" encoding="GBK"?>
<project name="pub_busi_plat" default="build" basedir=".">
	
	<property name="src" value="src" ></property>
	<property name="lib" value="WebRoot/WEB-INF/lib" ></property>
	<property name="script" value="script" ></property>
	<property name="config" value="config" ></property>
	<property name="classes" value=".build" ></property>
	
	<path id="Test_ouyangping.classpath" >
			<pathelement location="WebRoot/WEB-INF/classes" />
			<fileset dir="WebRoot/WEB-INF/lib" includes="*.jar" />
	</path>
	
	<target name="clean">
		<delete dir="${classes}" ></delete>
	</target>
	
	<target name="init">
			<mkdir dir="${classes}"/>
			<mkdir dir="${classes}/bin"/>
			<mkdir dir="${classes}/lib"/>
			<mkdir dir="${classes}/script"/>
			<mkdir dir="${classes}/log"/>
			<touch file="${classes}/log/system.log" > </touch> <!-- 创建文件 -->
	</target>
	
	<target name="compile">
		<javac srcdir="${src}" destdir="${classes}/bin" debug="no" >
			<classpath refid="Test_ouyangping.classpath" ></classpath>
		</javac>
	</target>
	
	<target name="copy">
		<copy todir="${classes}/bin" >
				<fileset dir="${src}" >
					<include name="**/*.xml" />
				</fileset>
				<fileset  dir="${config}" >
					<include name="**/*" />
				</fileset>
		</copy>
		<copy todir="${classes}/lib" >
			<fileset dir="${lib}">
				<include name="*.jar" />
			</fileset>
		</copy>
		<copy todir="${classes}/script">
			<fileset dir="${script}" >
				<include name="*" />
			</fileset>	
		</copy>
	</target>
	<target name="build" depends="clean,init,compile,copy" ></target>
</project>

 

 

<replace dir="${classes.home}/bin/com/umpay/tsm/db/sql" includes="*.xml" encoding="UTF-8">

<replacefilter token="@SCHEMA@" value="${schema}"/>

</replace> 可替换文本

 

javac指定jdk版本:http://neoman.iteye.com/blog/571806

为 javac 任务指定target属性,取值为:1.3、1.4、1.5 即自己需要编译源码的版本

 

 

   #自动执行测试代码,并输出测试代码报告:

 

 <target name="junitTest" depends="build"> 
				 <junit printsummary="on" fork="true" showoutput="true"> 
					 <classpath> 
					 	<!--
						 <fileset dir="lib" includes="**/*.jar"/> 
						 <pathelement path="${junit.classpath}"/> 
					 	-->
							<pathelement location="bin" />
							<fileset dir="lib" includes="*.jar" />
					 </classpath> 
					 <formatter type="xml" /> 
					 <batchtest todir="${testResut}"> 
						 <fileset dir="${test}"> 
							 <include name="**/*.*" /> 
						 </fileset> 
					 </batchtest> 
				 </junit> 
				 <junitreport todir="${testResut}"> 
				 	<!--
					 <fileset dir="${test}"> 
						 <include name="**/*,*" /> 
					 </fileset>
					 --> 
				 	  <fileset dir="${testResut}">
				 	                <include name="TEST-*.xml"/>
				 	            </fileset>
					 <report format="frames" todir="${testResut}" />
				 </junitreport> 
	 </target> 

 

 

 

猜你喜欢

转载自pouyang.iteye.com/blog/1146855
今日推荐