Ant 简明教程
Ant - Property Task
Ant 构建文件以 XML 编写,这不允许您像在您喜欢的编程语言中那样声明变量。不过,您可能已经想象到了,如果 Ant 允许声明变量(如项目名称、项目源目录等),那将很有用。
Ant build files are written in XML, which does not allow declaring variables as you do in your favorite programming language. However, as you may have imagined, it would be useful if Ant allowed declaring variables such as project name, project source directory, etc.
Ant 使用 property 元素,它允许你指定属性。这允许属性在不同的构建或不同的环境中更改。
Ant uses the property element which allows you to specify the properties. This allows the properties to be changed from one build to another or from one environment to another.
Ant Properties
默认情况下,Ant 提供了以下预定义的属性,这些属性可以在构建文件中使用:
By default, Ant provides the following pre-defined properties that can be used in the build file −
Sr.No |
Properties & Description |
1 |
ant.file The full location of the build file. |
2 |
ant.version The version of the Apache Ant installation. |
3 |
basedir The basedir of the build, as specified in the basedir attribute of the project element. |
4 |
ant.java.version The version of the JDK that is used by Ant. |
5 |
ant.project.name The name of the project, as specified in the name attribute of the project element. |
6 |
ant.project.default-target The default target of the current project. |
7 |
ant.project.invoked-targets Comma separated list of the targets that were invoked in the current project. |
8 |
ant.core.lib The full location of the Ant jar file. |
9 |
ant.home The home directory of Ant installation. |
10 |
ant.library.dir The home directory for Ant library files - typically ANT_HOME/lib folder. |
Ant 也使系统属性(示例:file.separator)可用于构建文件。
Ant also makes the system properties (Example: file.separator) available to the build file.
除了以上内容,用户可以使用 property 元素定义附加的属性。
In addition to the above, the user can define additional properties using the property element.
以下示例展示了如何定义一个名为 sitename 的属性:
The following example shows how to define a property called sitename −
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<property name="sitename" value="www.tutorialspoint.com"/>
<target name="info">
<echo>Apache Ant version is ${ant.version} - You are at ${sitename} </echo>
</target>
</project>
Output
在上述构建文件上运行 Ant 会生成以下输出:
Running Ant on the above build file produces the following output −
C:\>ant
Buildfile: C:\build.xml
info: [echo] Apache Ant version is Apache Ant(TM) version 1.10.12
compiled on October 13 2021 - You are at www.tutorialspoint.com
BUILD SUCCESSFUL
Total time: 0 seconds
C:\>