参考的网上的一个例子,自己加了一些注释:
<?xml version="1.0" encoding="UTF-8"?>
<!---->
<!--project name可任意取,默认运行的target是run -->
<project name="HelloWorld" default="run" basedir=".">
<!--声明变量-->
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<property name="hello_jar" value="hello.jar"/>
<!--创建src目录-->
<target name="init">
<mkdir dir="${dest}"/>
</target>
<!--编译,complie依赖于init-->
<!--includeantruntime为可选项,不设置的话,会报warning,详情参考:http://ant.apache.org/manual/Tasks/javac.html -->
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" includeantruntime="false"/>
</target>
<!--打包-->
<target name="build" depends="compile">
<jar jarfile="${hello_jar}" basedir="${dest}"/>
</target>
<!--运行-->
<target name="run" depends="build">
<java classname="HelloWorld" classpath="${hello_jar}"/>
</target>
<!--
<target name="clean">
<delete dir="${dest}" />
<delete file="${hello_jar}" />
</target>
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
-->
</project>
HelloWorld.java :
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello world1");
}
}