Apache Ant is a Java-based build tool. A java library and command line tool, which aims to drive processes described in build files as targets and extension points dependent upon each other.

Main usage: build java applications. It can pilot any type of processes which can be described in terms of targets and tasks.

1. Writing a Simple BuildFile

1.1 Intro

Ant’s buildfile are written in XML. Each buildfile contains one project and as least one target. Targets contain task elements. Each task element of the build file can have an id attribute and can later be referred to by the value supplied to this.

The structure comes to be:

  • project
  • target
    • task elements
      • id attribute

1.2 Projects

A project has three attributes:

  • name
    • the name of project
  • default
    • the default target to use when no target is supplied
  • basedir
    • the base directory from which all path calculations are done. A relative path is resolved relative to the directory containing the buildfile.
    • defaults to the parent directory of the buildfile

1.3 Targets

It’s a container of tasks that cooperate to reach a desired state during the build process.

A target can depend on other targets. **You might have a target for compiling, for example, and a target for creating a distributable. You can only build a distributable when you have compiled first, so the “distribute” target depends on the “compile” target. Ant resolves these dependencies

<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>

Suppose we want to execute target D. From its depends attribute, you might think that first target C, then B and then A is executed. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.

A target also has the ability to perform its execution if(or unless) a property has been set.

<target name="myTarget" depends="myTarget.check" if="myTarget.run">
    <echo>Files foo.txt and bar.txt are present.</echo>

<target name="myTarget.check">
    <condition property="myTarget.run">
            <available file="foo.txt"/>
            <available file="bar.txt"/>

1.4 Tasks

1.4.1 Intro

Task is a piece of code that can be executed.

A task can have multiple attributes. The value of an attribute might contain reference to a property. These references will be resolved before the task is executed.

Task has a common structure:

<name attribute1="value1" attribute2="value2" ... />

where name is the name of the task, attributeN is the attribute name, and valueN is the value for this attribute.

All tasks can have a name attribute, which will be used in the logging messages generated by Ant.

1.4.2 Built-in tasks


1.4.3 Write your own tasks


1.5 Properties

Properties are an important way to customize a build process or to just provide shortcuts for strings that are used repeatedly inside a buildfile.

In its most simple form properties are defined in the buildfile (for example by the property task) or might be set outside Ant. A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes or in the nested text of tasks that support them. This is done by placing the property name between “${“ and “}” in the attribute value. For example, if there is a builddir property with the value “build”, then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.

<project name="MyProject" default="dist" basedir=".">
    simple example build file
  <!-- 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 -->
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>

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

  <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 name="clean"
        description="clean up">
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>

Properties are key-value pair where Apache Ant tries to expand ${key} to value at run time. In general properties are of global scope, i.e., once they have been defined they are available for any task or target invoked subsequently—it is not possible to set a property in a child build process created via the ant, antcall or subant tasks and make it available to the calling build process, though.

1.5.2 Built-in Properties

Ant provides access to all system properties as if they had been defined using a task.

  • javadoc of System.getProperties
  • basedir: the absolute path of the project’s basedir
  • ant.file: the absolute path of the buildfile
  • ant.version
  • ant.project.name

2. Concepts and Types

2.1 Concepts

  • targets and Extension-points
  • Properties and PropertyHelpers

2.2 List of Types


  • Class Fileset
  • Description Type
  • Directory based Tasks
  • Dirset
  • Extension Package
  • Set of Extension Packages
  • FileList
  • FileSet
  • File Mappers
  • FilterChains and FilterReaders
  • FilterSet
  • MultiRootFileSet
  • PatternSet
  • Path-like Structures
  • Permissions
  • PropertySet
  • I/O redirectors
  • Regexp
  • Resources
  • Resource Collections
  • Selectors
  • TarFileSet
  • XMLCatalog
  • ZipFileSet

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 stone2paul@gmail.com



本文作者:Leilei Chen

发布时间:2020-02-05, 00:32:50

最后更新:2020-02-05, 00:33:18


版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。