Ant 简明教程
Ant - Build Files
通常,Ant 构建文件名为 build.xml ,它应当放在项目的基本目录中。然而,对文件名或它的位置没有限制。您可以自由地使用其它文件名或将构建文件保存在其它位置。
Typically, Ant’s build file, called build.xml should reside in the base directory of the project. However, there is no restriction on the file name or its location. You are free to use other file names or save the build file in some other location.
为了进行这个练习,在计算机中任意位置创建一个名为 build.xml 的文件,它的内容如下:
For this exercise, create a file called build.xml anywhere in your computer with the following contents −
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
请注意,xml 声明之前不应有空行或空格。如果您允许空行或空格,那么在执行 ant 构建时,会导致以下错误信息:
Note that there should be no blank line(s) or whitespace(s) before the xml declaration. If you allow them, the following error message occurs while executing the ant build −
The processing instruction target matching "[xX][mM][lL]" is not allowed.
All build files require the project element and at least one target element.
XML 元素 project 有三个属性,如下:
The XML element project has three attributes which are as follows −
Sr.No |
Attributes & Description |
1 |
name The Name of the project. (Optional) |
2 |
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) |
3 |
basedir The base directory (or) the root folder for the project. (Optional) |
目标是您要作为一个单元运行的任务集合。在我们的示例中,我们有一个简单的目标,为用户提供信息消息。
A target is a collection of tasks that you want to run as one unit. In our example, we have a simple target to provide an informational message to the user.
目标可以依赖于其他目标。例如, deploy 目标可能依赖于程序包目标, package 目标可能依赖于编译目标,依此类推。依赖项使用 depends 属性表示。
Targets can have dependencies on other targets. For example, a deploy target may have a dependency on the package target, the package target may have a dependency on the compile target and so forth. Dependencies are denoted using the depends attribute.
例如 -
For example −
<target name="deploy" depends="package">
....
</target>
<target name="package" depends="clean,compile">
....
</target>
<target name="clean" >
....
</target>
<target name="compile" >
....
</target>
目标元素具有以下属性:
The target element has the following attributes −
Sr.No |
Attributes & Description |
1 |
name The name of the target (Required) |
2 |
depends Comma separated list of all targets that this target depends on. (Optional) |
3 |
description A short description of the target. (optional) |
4 |
if Allows the execution of a target based on the trueness of a conditional attribute. (optional) |
5 |
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 。
The echo task in the above example is a trivial task that prints a message. In our example,it prints the message Hello World.
要运行蚂蚁构建文件,请打开命令提示符并导航到 build.xml 所在的文件夹,然后键入 ant info 。你也可以键入 ant 。两者都会起作用,因为 info 是构建文件中的默认目标。
To run the ant build file, open up command prompt and navigate to the folder, where the build.xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.
你应该看到以下输出:
You should see the following output −
C:\>ant
Buildfile: C:\build.xml
info: [echo] Hello World - Welcome to Apache Ant!
BUILD SUCCESSFUL
Total time: 0 seconds
C:\>