Ant Build example.

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="usage" name="TestPrjAndBuild">
	<!-- 定义目录变量 -->
	<property name="project-name" value="TestPrjAndBuild" /> <!-- ====此处需要修改====.jar文件名 -->
	<property name="src.dir" value="src" /> <!-- src -->
	<property name="lib.dir" value="lib" /> <!-- lib -->
	<property name="build.dir" value="bin" /> <!-- bin -->

	<property name="jar-file-name" value="${project-name}.jar" />
	<property name="main-class" value="com.test.MyMain" /> <!-- ====此处需要修改====main-class -->
	<!-- 定义classpath -->
	<path id="master-classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<pathelement path="${build.dir}"></pathelement>
	</path>
	<target description="Build file usage info (default task)" name="usage"> <!-- 默认执行的Task -->
		<echo message=" " />
		<echo message="  ${project-name} " />
		<echo message="-------------------------------------------------------" />
		<echo message="  Available Targets:" />
		<echo message="  init    	- Init the directory" />
		<echo message="  compile    - Compiles the source code" />
		<echo message="  copy-res   - Copy the resources" />
		<echo message="  jar        - Generate an .jar for source code" />
		<echo message="  run        - Execute Main-Class" />
		<echo message="  clean      - Delete class files and .jar file" />
		<echo message="-------------------------------------------------------" />
	</target>

	<!-- 初始化${build.dir}目录 -->
	<target name="init">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${lib.dir}" />
		<!-- 是否有lib目录,没有则加上 -->
	</target>
	<!-- 编译 -->
	<target depends="init" name="编绎">
		<javac encoding="UTF-8" debug="true" deprecation="on" destdir="${build.dir}"
			listfiles="on" failonerror="true" srcdir="${src.dir}">
			<classpath refid="master-classpath" />
		</javac>

	</target>
	<!-- 复制资源文件 -->
	<target name="复制资源文件">
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>

	<!-- 运行 -->
	<target depends="编绎" name="运行">
		<java classname="${main-class}" classpath="${build.dir}" fork="true"
			maxmemory="256m">
			<classpath refid="master-classpath" />
			<sysproperty key="file.encoding" value="UTF-8" />
			<arg value="-h" />
		</java>
	</target>
	<!-- 生成jar包 -->
	<target depends="编绎" name="打成jar包">
		<jar basedir="${build.dir}" destfile="${jar-file-name}">
			<manifest>
				<attribute name="Main-Class" value="${main-class}" />
				<attribute name="Class-Path" value="" />
			</manifest>
		</jar>
	</target>
	<!-- 清除 -->
	<target name="clean">
		<delete dir="${build.dir}" />
		<delete file="${jar-file-name}" />
	</target>
</project>

posted @ 2011-04-01 20:04  庚武  Views(325)  Comments(0Edit  收藏  举报