【转】ant 安装与配置

下载Apache Ant 1.8.4http://ant.apache.org/bindownload.cgi

一、解压ant安装包在D:\SWE下

二、环境变量配置

ANT_HOME D:\SWE\apache-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:\Program Files\Java\jre6\lib

命令行敲ant命令后提示:“Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib”;ANT_HOME环境变量已经配置;

解决途径:将“C:\Program Files\Java\jdk1.6.0_16\lib”目录下的tools.jar文件拷贝到“C:\Program Files\Java\jre6\lib”目录下,重新运行命令ant,运行正常,问题解决。

2)在cmd命令中:输入ant,如果输出:

Buildfile:build.xml does not exist!
Build failed

说明ant安装成功。

、运行第一个ant脚本 

在D:\ant_home\apache-ant-1.8.1\bin\下面新建目录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_home\apache-ant-1.8.1\bin\ 
内容如下: 
  

<?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}] ------------" /> 
  <target name="init"> 
  <echo message="mkdir ${dest}"></echo> 
  <mkdir dir="${dest}" /> 
  </target> 
  <target name="compile" depends="init" description="Compile Java code"> 
  <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> 
  <target name="clean"> 
  <delete dir="${dest}" /> 
  <delete file="${hello_jar}" /> 
  </target> 
  </project> 

 

运行: 

Buildfile: D:\ant_home\apache-ant-1.8.1\bin\build.xml 
  [echo] ----------- HelloWorld 1.0 [2010] ------------ 
  init: 
  [echo] mkdir build/classes 
  compile: 
  [javac] Compiling 1 source file to D:\ant_home\apache-ant-1.8.1\bin\build\classes 
  build: 
  [jar] Building jar: D:\ant_home\apache-ant-1.8.1\bin\build\hello.jar 
  run: 
  [java] Hello World 
  BUILD SUCCESSFUL 
  Total time: 1 second 

检查在目录D:\ant_home\apache-ant-1.8.1\bin\build下生成hello.jar 

 

原文链接:http://blog.sina.com.cn/s/blog_62ef85c201016e2n.html 

posted @ 2013-11-03 21:35  库乐君  阅读(289)  评论(0编辑  收藏  举报