下载Apache Ant 1.8.4http://ant.apache.org/bindownload.cgi
一、解压ant安装包在D:SWE下
二、环境变量配置
ANT_HOME D:SWEapache-ant-1.8.4
CLASSPATH ;%ANT_HOME%lib;
PATH ;%ANT_HOME%bin;
三、测试是否安装成功
在cmd命令方式下输入:ant -version
出现问题:
1)Unable to locate tools.jar. Expected to find it in C:ProgramFilesJavajre6lib
命令行敲ant命令后提示:“Unable to locate tools.jar. Expected to find it inC:Program FilesJavajre6lib”;ANT_HOME环境变量已经配置;
解决途径:将“C:ProgramFilesJavajdk1.6.0_16lib”目录下的tools.jar文件拷贝到“C:ProgramFilesJavajre6lib”目录下,重新运行命令ant,运行正常,问题解决。
2)在cmd命令中:输入ant,如果输出: Buildfile:build.xml does not exist!
Build failed
说明ant安装成功。
四、运行第一个ant脚本
在D:ant_homeapache-ant-1.8.1bin下面新建目录build,再在该目录下新建目录src
同时在src目录下新建HelloWorld.java
内容如下:
package test.ant;
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");
}
};
编写build.xml文件保存到D:ant_homeapache-ant-1.8.1bin
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld" default="run"basedir=".">
<property name="src" value="build/src"/>
<property name="dest" value="build/classes"/>
<property name="hello_jar" value="hello.jar"/>
<property name="name" value="HelloWorld"/>
<property name="version" value="1.0"/>
<property name="year" value="2010"/>
<echo message="----------- ${name} ${version}[${year}] ------------"/>
<targetname="init">
<echo message="mkdir${dest}"></echo>
<mkdir dir="${dest}"/>
</target>
<target name="compile" depends="init"description="Compile Javacode">
<javac srcdir="${src}" destdir="${dest}"includeantruntime="on"/>
</target>
<target name="build"depends="compile">
<jar jarfile="build/${hello_jar}"basedir="${dest}"/>
</target>
<target name="run"depends="build">
<java classname="test.ant.HelloWorld"classpath="build/${hello_jar}"/>
</target>
<targetname="clean">
<delete dir="${dest}"/>
<delete file="${hello_jar}"/>
</target>
</project>
运行:
Buildfile:D:ant_homeapache-ant-1.8.1binbuild.xml
[echo] ----------- HelloWorld 1.0 [2010]------------
init:
[echo] mkdir build/classes
compile:
[javac] Compiling 1 source file toD:ant_homeapache-ant-1.8.1binbuildclasses
build:
[jar] Building jar:D:ant_homeapache-ant-1.8.1binbuildhello.jar
run:
[java] Hello World
BUILD SUCCESSFUL
Total time: 1 second
检查在目录D:ant_homeapache-ant-1.8.1binbuild下生成hello.jar