2006-6-24

Installing Ant
The binary distribution of Ant consists of the following directory layout:

  ant
   +--- bin  // contains launcher scripts
   |
   +--- lib  // contains Ant jars plus necessary dependencies
   |
   +--- docs // contains documentation
   |      +--- ant2    // a brief description of ant2 requirements
   |      |
   |      +--- images  // various logos for html documentation
   |      |
   |      +--- manual  // Ant documentation (a must read ;-)
   |
   +--- etc // contains xsl goodies to:
            //   - create an enhanced report from xml output of various tasks.
            //   - migrate your build files and get rid of 'deprecated' warning
            //   - ... and more ;-)

Only the bin and lib directories are required to run Ant. To install Ant, choose a directory and copy the distribution file there. This directory will be known as ANT_HOME.
在安装ant的时候,需要两个环境变量。ANT_HOME指定ant的安装目录,Path指定bin的目录路径。
-------------------------------------

Using Ant
Each Buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this.

A project has three attributes: name (required no), default(the default target to use when no target is supplied) (required no), basedir (required no).
Optionally, a description for the project can be provided as a top-level <description> element. It is include in the output of the ant - projecthelp command. The description has no parameters. One example:
<description>
This buildfile is used to build the Foo subproject within
the large, complex Bar project.
</description>

Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed.
A target has the following attributes: name (required yes), depends, if, unless, description.

A task is a piece of code that can be executed.
Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />

Example Buildfile
<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
--------------------------------------

Running Ant
Examples
ant
runs Ant using the build.xml file in the current directory, on the default target.

ant -buildfile test.xml
runs Ant using the test.xml file in the current directory, on the default target.

ant -buildfile test.xml dist
runs Ant using the test.xml file in the current directory, on the target called dist.
-------------------------------------------

上面的内容都是从manual中copy出来的,觉得我用到的特征应该不多。会按装、会写简单的build.xml、以及运行ant就可以了。Ant提供的tasks很多,可能大部分都是复杂工程当中所需的,但是对于我个人写的小projiect,这些tasks就显得没有必要了。简单是一种美德:-),这也是懒得借口之一。
--------------------------------------------

Developing with Ant
Tutorials
Hello World with Ant

We want to separate the source from the generated files, so our java source files will be in src folder. All generated files should be under build, and there splitted into several subdirectories for the individual steps: classes for our compiled files and jar for our own JAR-file.
The later directories are created by our buildfile, so we have to create only the src directory.
write this code into src/oata/HelloWorld.java 代码如下:
  package oata;
  public class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello World");
      }
  }

The most simplest buildfile describing that would be:写一个最简单的build.xml:
<project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="oata.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>

Now you can compile, package and run the application via

ant compile
ant jar
ant run

Or shorter with

ant compile jar run

完善build file:
<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="oata.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

Now it's easier, just do a ant and you will get

Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: C:\...\build\classes
    [javac] Compiling 1 source file to C:\...\build\classes

jar:
    [mkdir] Created dir: C:\...\build\jar
      [jar] Building jar: C:\...\build\jar\HelloWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL

========这下面是我尝试了这例子后在cmd中的输出==========
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

D:\doctemp\project helloworld using ant>ant
Buildfile: build.xml

clean:
   [delete] Deleting directory D:\doctemp\project helloworld using ant\build

compile:
    [mkdir] Created dir: D:\doctemp\project helloworld using ant\build\classes
    [javac] Compiling 1 source file to D:\doctemp\project helloworld using ant\b
uild\classes

jar:
    [mkdir] Created dir: D:\doctemp\project helloworld using ant\build\jar
      [jar] Building jar: D:\doctemp\project helloworld using ant\build\jar\Hell
oWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL
Total time: 2 seconds
D:\doctemp\project helloworld using ant>

=====================================================

此后还讲了使用外部libraries的例子,这里省去。