---
title: "Ant的配置使用入门 "
author: "aijcjo"
date: "2010-07-16T09:53:31Z"
tags: ["Ant", "C", "C#", "C++", "Java"]
categories: ["编程语言"]
slug: "ant"
draft: false
lastmod: "2010-07-16T09:53:31Z"
id: 714096
url: "https://aijcjo.iteye.com/blog/714096"
---

1、[下载](http://download.chinaitlab.com/)安装[下载](http://download.chinaitlab.com/)：http://ant.apache.org

    需要设置的环境变量：

    ANT\_HOME：ant的安装目录

    JAVA\_HOME：jdk的安装目录

    PATH：把%ANT\_HOME%\\bin目录加到path变量，以便于从命令行下直接运行ant

    假定ant解压在c：\\ant   jdk装d：\\j2sdk1.4.0

    则在命令行下执行以下命令：

set ANT\_HOME=c:\\ant  
set JAVA\_HOME=D:\\j2sdk1.4.0  
set PATH=%PATH%;c:\\ant\\bin

    2、工程文件目录结构：

    一个ant 工程目录结构：

    C：\\workspace\\ant.test：工程主目录

    \\src ：源程序目录

    \\build\\classes ：编译后的 class 文件目录

    \\lib ：开发工程所需要的类库目录，比如开发数据库时所需要的 jdbc lib（这次没用到）

    \\jar ：打包好的 jar 程序目录（这次没用到）

    \\build.xml ：工程配置文件\\build.propertiees：工程资源文件

    2、建立工程描述文件和建立工程资源文件

    建立工程描述文件build.xml

&lt;?xml version="1.0"?&gt;

&lt;project default="main" basedir="."&gt;

    &lt;echo message="pulling in property files" /&gt;  
    &lt;property file="build.properties" /&gt;

    &lt;target name="init"&gt;  
        &lt;echo message="init. delete the old class files. and create the new folds." /&gt;  
        &lt;delete dir="${classpath}" /&gt;  
        &lt;mkdir dir="${classpath}" /&gt;  
    &lt;/target&gt;

    &lt;target name="compile" depends="init"&gt;  
        &lt;echo message="compile the java source files." /&gt;  
        &lt;javac srcdir="src\\hello\\ant" destdir="${classpath}" /&gt;  
    &lt;/target&gt;

    &lt;target name="main" depends="compile"&gt;  
        &lt;echo message="calling java to run this java project." /&gt;  
        &lt;java classname="hello.ant.HelloAnt"&gt;  
            &lt;classpath&gt;  
                &lt;pathelement path="${classpath}" /&gt;  
            &lt;/classpath&gt;  
        &lt;/java&gt;  
    &lt;/target&gt;

&lt;/project&gt;

    建立工程资源文件：build.properties

    文件内容是下面一行内容：

`classpath=build\\classes`

    4、建立java源文件：hello.ant.HelloAnt.java

`package hello.ant; public class HelloAnt {     public static void main(String[] args) {         System.out.println("hello ant, the first time using ant... it is great.");     } }`

    5、编译：

C:\\workspace\\ant.test&gt;ant -buildfile build.xml  
Buildfile: build.xml  
     \[echo] pulling in property files

init:  
     \[echo] init. delete the old class files. and create the new folds.  
   \[delete] Deleting directory C:\\workspace\\ant.test\\build\\classes  
    \[mkdir] Created dir: C:\\workspace\\ant.test\\build\\classes

compile:  
     \[echo] compile the java source files.  
    \[javac] Compiling 1 source file to C:\\workspace\\ant.test\\build\\classes

main:  
     \[echo] calling java to run this java project.  
     \[java] hello ant, the first time using ant... it is great.

BUILD SUCCESSFUL  
Total time: 890 milliseconds

C:\\workspace\\ant.test&gt;