Ant 简明教程
Ant - Property Files
如果只需使用少数几个属性,则直接在构建文件中设置属性就足够了。但是,对于大型项目而言,将属性存储在单独的属性文件中有意义。
Setting properties directly in the build file is fine, if you are working with a handful of properties. However, for a large project, it makes sense to store the properties in a separate property file.
Benefits
将属性存储在单独的文件中会带来以下好处:
Storing the properties in a separate file offers the following benefits −
-
It allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST, and PROD environments.
-
It is useful, when you do not know the values for a property (in a particular environment) up-front. This allows you to perform the build in other environments, where the property value is known.
没有硬性规定,但通常属性文件被命名为 build.properties ,并与 build.xml 文件放在一起。你可以根据部署环境创建多个构建属性文件,比如 build.properties.dev 和 build.properties.test 。
There is no hard and fast rule, but typically the property file is named as build.properties and is placed along-side the build.xml file. You could create multiple build properties files based on the deployment environments - such as build.properties.dev and build.properties.test.
构建属性文件的文本内容与规范的 java 属性文件类似。它们每行包含一个属性。每个属性由名称和值对表示。
The contents of the build property file are similar to the normal java property file. They contain one property per line. Each property is represented by a name and a value pair.
名称和值对用等号 (=) 符号分隔。强烈建议使用正确的注释为这些属性做注解。使用井号 (#) 字符列出注释。
The name and value pairs are separated by an equals (=) sign. It is highly recommended that the properties are annotated with proper comments. Comments are listed using the hash (#) character.
以下示例展示了一个 build.xml 文件及其关联 build.properties 文件−
The following example shows a build.xml file and its associated build.properties file −
build.xml
以下是针对 build.xml 文件的示例。
Given below is an example for build.xml file.
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<property file="build.properties"/>
<target name="info">
<echo>Apache Ant version is ${ant.version} - You are at ${sitename} </echo>
</target>
</project>
build.properties
以下是针对 build.properties 文件的示例 −
An example for build.properties file is mentioned below −
# The Site Name
sitename=www.tutorialspoint.com
buildversion=3.3.2
在上述示例中, sitename 是映射到网站名称的自定义属性。您可以用这种方式声明任意数量的自定义属性。
In the above example, sitename is a custom property which is mapped to the website name. You can declare any number of custom properties in this fashion.
上述示例中列出的另一个自定义属性是 buildversion ,它在这种情况下指代构建版本。
Another custom property listed in the above example is the buildversion, which, in this instance, refers to the version of the build.
除上述内容外,Ant 内含若干预定义构建属性,已在上一部分中列出,但在此再次列出以供参考。
In addition to the above, Ant comes with a number of predefined build properties, which are listed in the previous section, but is given below once again for your reference.
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.version 内置属性。
The example presented in this chapter uses the ant.version built-in property.