Ant構建文件

通常情況下,Ant構建文件build.xml應該在項目的基礎目錄。可以自由使用其他文件名或將構建文件中其他位置。

在本練習中,創建一個名爲build.xml 在電腦的任何地方的文件。

<project name="Hello World Project" default="info"> <target name="info"> Hello World - Welcome to Apache Ant!

請注意,應該有XML聲明之前沒有空行或空格。該處理指令目標匹配"[xX][mM][lL]" 是不允許的 - 如果你這樣做,這可能在運行Ant構建時造成的錯誤消息。

所有構建文件要求項目元素和至少一個目標元素。

XML元素的項目有三個屬性:

屬性

描述

name

The Name of the project. (Optional)

default

The default target for the build script. A project may contain any number of targets. This attribute specifies which target should be considered as the default. (Mandatory)

basedir

The base directory (or) the root folder for the project. (Optional)

一個目標是要作爲一個單元運行的任務的集合。在我們的例子中,我們有一個簡單的目標,以提供一個信息性消息給用戶。

目標可以對其他目標的依賴關係。例如,部署目標可能對封裝對象的依賴和包的目標可能具有依賴於compile目標等等。依賴關係是使用依賴屬性表示。例如:

<target name="deploy" depends="pacakge"> .... <target name="pacakge" depends="clean,compile"> .... <target name="clean" > .... <target name="compile" > ....

目標元素具有以下屬性:

屬性

描述

name

The name of the target (Required)

depends

Comma separated list of all targets that this target depends on. (Optional)

description

A short description of the target. (optional)

if

Allows the execution of a target based on the trueness of a conditional attribute. (optional)

unless

Adds the target to the dependency list of the specified Extension Point. An Extension Point is similar to a target, but it does not have any tasks. (Optional)

在上面的例子中的echo 任務是打印一條消息一個簡單的任務。在我們的例子,它打印出Hello World消息。

要運行Ant構建文件,打開命令提示符並導航到build.xml文件所在的文件夾,然後輸入ant info。也可以只輸入ant來代替。既會工作,因爲信息是默認的目標在構建文件。應該看到下面的輸出:

C:>ant Buildfile: C:uild.xml

info: [echo] Hello World - Welcome to Apache Ant! BUILD SUCCESSFUL Total time: 0 seconds

C:>