Automatic Build tool ant in-depth analysis (2) Ant attributes, goals, and dependencies

Source: Internet
Author: User
First, let's look at a sample XML file: <? XML version = "1.0"?> <Project default = "init" name = "project"> <description> A simple project introducing the use of descriptive tags in ant build files. </description> <! -- XML comments can also be used --> <target name = "init" Description = "initialize argon Database"> <! -- Perform initialization steps here --> </Target> </Project>

We can see that XML annotations can be used throughout the generated file to improve clarity. Ant also defines its own description elements and description attributes, which can be used to provide more structured comments.

Ant attributes:

Attributes in ant are similar to variables in programming languages. They all have names and values. However, unlike common variables, attributes in ant cannot be changed once set; they are immutable, just like string objects in Java. This seems restrictive at first, but it is to follow ant's simple principle: after all, it is a generation tool, not a programming language. If you try to assign a new value to an existing property, it is not considered as an error, but the property still retains its existing value.


Define and use attributes:

<Property name = "Metal" value = "beryllium"/>

To reference this attribute in other parts of the generated file, use the following syntax:
$ {Metal}

For example, to use such a value, it is an integral part of the value of another property.

<property name="metal-database" value="${metal}.db"/>

Location Property:

Attributes are often used to reference files or directories on the file system. However, for platforms that use different path delimiters (for example,/and \), this may cause problems when crossing different platforms. The location attribute of ant is designed to include the file system path in a platform-independent manner. Use location to replace value as follows:

<Property name = "database-file" location = "Archive/databases/$ {Metal}. DB"/>

The path delimiter used for the location attribute is converted to the correct format of the current platform. Because the file name is relative, it is considered to be relative to the project's base directory. We can also easily write as follows:

<Property name = "database-file" location = "ARCHIVE \ databases \ $ {Metal}. DB"/>

Both versions of this tag share the same behavior on different platforms.


Define dependency:

It usually takes many steps to generate a project. For example, you must first compile the source code and then package it into a Java archive file.
(Java archive file, Jar ). Many of these steps have clearly defined sequence-for example, you cannot package class files before the compiler generates class files from the source code. Unlike specifying the target in sequence, ant uses a more flexible method to define dependencies. Each target is defined based on all other goals that must be completed before it can be executed. This is implemented using the depends attribute of the target element.

<Target name = "init"/>

<Target name = "preprocess" depends = "init"/>

<Target name = "compile" depends = "init, preprocess"/>

<Target name = "package" depends = "compile"/>

This method allows you to execute the build process at any stage of the project; ant will first execute the defined prerequisite stage. In the preceding example, if ant completes the compile step, it determines that the init and preprocess targets must be executed first. The init target does not depend on any other target, so it will be executed first. Then ant checks preprocesstarget and finds that it depends on the init target. Since the latter has been executed, ant will not execute it again, so it starts to execute the preprocess target. Finally, you can execute the compile task itself.

Note that the sequence in which the target appears in the generated file is not important: the execution sequence is uniquely determined by the depends attribute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.