日期:2014-05-20  浏览次数:20572 次

一个完整的ant-j2se例子
<?xml version="1.0" ?>
<project name="structured" default="all" basedir=".">
   <!-- all 代表执行 target为all 的任务 , 如果 all中 execute 任务没有包含,则不会执行-->
	<description>Compiles and runs a simple program</description>

	<property name="app.name" value="AntProject" />
	<property name="app.jar" value="${app.name}.jar" />

	<property name="lib.dir" value="lib" />
	<property name="src.dir" location="src" />


	<property name="build.dir" location="build" />
	<property name="dist.dir" location="dist" />

	<target name="init">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${dist.dir}" />
		<mkdir dir="${build.dir}/lib" />
	</target>

	<target name="compile" depends="init" description="Compiles the source code">
		<javac srcdir="${src.dir}" destdir="${build.dir}" source="1.6" target="1.6" debug="on" />  <!-- 调试模式开启 -->
		<copy todir="${build.dir}">  <!-- 拷贝文件 todir 目标文件 -->
			<fileset dir="${src.dir}">
				<include name="*.xml" />
				<include name="*.properties" />
			</fileset>
		</copy>
		<copy todir="${build.dir}/lib">
			<fileset dir="${lib.dir}">
				<include name="**/*.jar" />
			</fileset>
		</copy>
	</target>

	<target name="dist" depends="compile" description="generate the distribution">
		<jar jarfile="${dist.dir}/${app.jar}" basedir="${build.dir}" >
		  <manifest>
		    <attribute name="Main-class" value="com.tyler4life.ant.HelloWorld"/>
		  </manifest>
		</jar>
		  <!-- 打包 -->
	</target>

	<target name="clean" description="Removes the temporary directories used">
		<delete dir="${build.dir}/lib">
		</delete>
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
	</target>

	<target name="execute" depends="compile" description="Runs the program">
		<echo level="warning" message="running" />
		<java classname="com.tyler4life.ant.HelloWorld" classpath="${build.dir}">
			<arg value="a" />
			<arg value="b" />
			<arg file="." />
		</java>
	</target>

	<target name="all" depends="clean,init,dist,execute" description="Clean,build,dist" />

</project>