1.下载压缩包apache-ant-1.8.3-bin.zip
2.编写HelloWorld.java
3.编写build.xml
<?xml version="1.0" encoding="utf-8"?>
<project name="HelloWorld" default="run" basedir=".">
<property name="src" value="."/>
<property name="dest" value="classes"/>
<property name="hello_jar" value="hello.jar"/>
<target name="init">
<mkdir dir="${dest}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</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>
</project>
4.若ant无设置环境变量,则需要cd到ant压缩包的bin目录下,在执行ant。同样将上述例子将build.xml和HelloWorld.java置于bin目录下。
5.build.xml解释:
src = "."为当前目录 mkdir 创建classes目录,
compile javac编译到classes目录下 build jar 对classes目录下的文件打包
run java执行jar