Apache Ant Operating Manual (2)

zhaozj2021-02-16  48

Use Ant

1 Write a simple compilation file

The compilation file of Ant is written in XML format, each compilation file contains a Project and at least one target. Targets contain some Task elements. Each Task element can have an ID attribute (Attribute) can be used by other references.

2 Projects

A Projext can have the following properties attribute:

Attribute

Description

Whether it must

Name

project name

NO

DEFAULT

When there is no Target support, the lack of use of Target

YES.

Basedir

The base catalog, other directories are relative to this. This Attribute can be overwritten by "Basedir" Property. Basedir Attribute doesn't work when "BaseDir" Property is set. If both are set, the parent directory of the BuildFile itself is used as the base directory.

NO

You can also set the instructions for the item as needed, in the element.

The first Project can have one or more Targets. A Target is a series of task groups you want to perform. When running Ant, you can choose which task group is executed. If no task group is given, the project default task group is executed.

3 Targets

Target may exist in other Targets. You may use a target to compile, use another target and used to deploy. Only after compiling is completed, the deployment can be implemented, so the deployment relies on compilation.

To explain, Ant's Depends Attribute just specifies the front-rear execution relationship between Targets and does not represent real dependence.

The order of dependence is from left to right in accordance with the order of arrangement. But if these dependencies, there is still dependencies, it is also possible to execute later, as follows:

If we are going to run Target D. From its dependence on the Attribute list, you may perform C, then B, then A. In fact, this is wrong. C depends on B, B depends on A. Therefore, A is executed first, but B, then C, finally D.

A Target is only executed once, although a lot of Target may depend on it. As shown in the above example.

The Target can also determine the execution based on its Attribute settings. This allows for a better control compilation process based on the condition of the system (such as Java version, OS, command line attribute definition, etc.). This introduces an IF or UNLESS attribute (Attribute).

Note: Ant is just check whether attribute is set, without checking its value, even if the attribute value is an empty string, it still thinks this is an attribute (Attribute). E.g:

In the first line, if the module-a-present attribute is set, the target is executed. In the second, if the module-a-present attribute is set, do not execute with Target.

If there is no IF or UNLESS attribute (Attribute), the target will be executed.

Optional Description attributes (attribute) can be used to indicate the description of the Target of the line, can be displayed in the 'token line with the -projecthelp parameter. TARGET without Description attribute (Attribute) is considered to be internal and is not displayed unless the -verbose or -debug parameter is used.

Initialize Target, all other Targets are dependent on it. It is usually located in the first item on the list. In this manual, most initialization Targets is named with init.

Target has the following properties:

Attribute

Description

Whether it must

Name

Target's name

YES

Depends

Relying on Target list

NO

IF

To perform Target, a property (attribute) must be set

NO

Unless

To perform Target, attribute must not be set

NO

Description

Short description for Target features

NO

TARGET Name Description: You can use any character in any XML encoded alphabet, such as a null string, comma, space. But it is best to avoid using them, in order to avoid arranging, future version of Ant does not support these special characters. The naming rules of Target are related to the specific development manufacturers when the Name Rules of the Target is related to specific development manufacturers.

The Target name starting with the horizontal line, such as "-restart" is a valid naming, but cannot be used to execute in the command line.

4 Tasks

TASK is an executable code.

Task can have a variety of attributes (or parameters, as long as you like). The value of attribute may contain references to attributes. These references should be parsed before TASK execution.

The general structure of TASK is as follows:

Name is the name of Task, Attribute is the name of the attribute, and Valuen is the value of attributes.

Some are built-in tasks, while others are optional tasks, and users can freely define.

All Tasks have shared task names, and the attribute value will be used when Ant generates a message.

Task can specify an ID attribute (attribute):

TaskName is the name of Task, TaskID is a unique identity. You can reference the corresponding Task object through this name in the scriptor or other Task. E.g: