您的位置:首页 > 其它

ANT教程之三 Ant构建文件

2015-11-07 16:18 281 查看
通常情况下,Ant构建文件build.xml应该在项目的基础目录。可以自由使用其他文件名或将构建文件中其他位置。

在本练习中,创建一个名为build.xml 在电脑的任何地方的文件。

<?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声明之前没有空行或空格。该处理指令目标匹配"[xX][mM][lL]" 是不允许的 - 如果你这样做,这可能在运行Ant构建时造成的错误消息。

所有构建文件要求项目元素和至少一个目标元素。

XML元素的项目有三个属性:
属性描述
nameThe Name of the project. (Optional)
defaultThe 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)
basedirThe base directory (or) the root folder for the project. (Optional)
一个目标是要作为一个单元运行的任务的集合。在我们的例子中,我们有一个简单的目标,以提供一个信息性消息给用户。

目标可以对其他目标的依赖关系。例如,部署目标可能对封装对象的依赖和包的目标可能具有依赖于compile目标等等。依赖关系是使用依赖属性表示。例如:
<target name="deploy" depends="pacakge">
....
</target>
<target name="pacakge" depends="clean,compile">
....
</target>
<target name="clean" >
....
</target>
<target name="compile" >
....
</target>

目标元素具有以下属性:

属性描述
nameThe name of the target (Required)
dependsComma separated list of all targets that this target depends on. (Optional)
descriptionA short description of the target. (optional)
ifAllows the execution of a target based on the trueness of a conditional attribute. (optional)
unlessAdds 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:>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: