ant示例

<?xml version="1.0" encoding="UTF-8" ?>
<project name="spring" default="run" basedir=".">

	<property name="src" value="src" />
	<property name="dest" value="classes" />

        <!-- 暂不生成jar包 -->	
        <property name="dest_jar" value="jar" />
	<property name="hello_jar" value="${dest_jar}/hello1.jar" />

        <!-- 定义类路径,在编译和执行时都用到,当前目录的lib目录下 -->
	<path id="classpath">
		<fileset dir="lib">
			<include name="**/*.jar" />
		</fileset>

	</path>

        <!-- 编译目录,先删后加,也可以(应该)不删,把depends='clean'去掉 -->
	<target name="init" depends="clean">
		<mkdir dir="${dest}" />
	</target>

	<target name="compile" depends="init">
		<!-- java源文件的编码最好指定 -->
                <javac encoding="utf-8" srcdir="${src}" destdir="${dest}" includeantruntime="on">
                        <!-- 编译时指定类路径 -->
			<classpath refid="classpath">
			</classpath>
		</javac>

		<!-- 这一步超重要,把非java文件也拷贝到类路径,如log4j.properties或xml文件等,ant会智能放到指定目录下,赞一个 -->
		<copy todir="${dest}">
			<fileset dir="${src}" excludes="**/*.java" />
		</copy>

	</target>

	<!-- 这里是basedir代表了编译的起始路径,暂时不用  -->
	<target name="build" depends="compile">
		<jar jarfile="${hello_jar}" basedir="${dest}" />
		<classpath refid="classpath">
		</classpath>
	</target>

	<target name="run" depends="compile">
	        <!-- 和编译不同,运行时多指定一个路径,类本身的路径 -->	 
                <java classname="onlyfun.caterpillar.FirstHibernate">
			<classpath>
				<pathelement location="${dest}" />
			</classpath>
			<classpath refid="classpath">
			</classpath>
		</java>
	</target>

	<target name="clean">
		<delete dir="${dest}" />
		<delete file="${hello_jar}" />
	</target>
</project>

猜你喜欢

转载自xieye.iteye.com/blog/1606839
ANT