Ant 简明教程

Ant - Data Types

Ant 提供了许多预定义的数据类型。不要将术语“数据类型”与编程语言中可用的数据类型混淆。相反,将它们视为已经内置到产品中的服务集。

Ant provides a number of predefined data types. Do not confuse the term "data types" with those that are available in the programming language. Instead, consider them as a set of services that are built into the product already.

Data Types in Ant

Apache Ant 提供了以下数据类型。

The following data types are provided by Apache Ant.

Fileset

文件集数据类型表示一组文件。它用作过滤器,用于包括或排除与特定模式匹配的文件。

The fileset data type represents a collection of files. It is used as a filter to include or exclude files that match a particular pattern.

例如,请参阅以下代码。此处,src 属性指向项目的源文件夹。

For example, refer the following code. Here, the src attribute points to the source folder of the project.

<fileset dir="${src}" casesensitive="yes">
   <include name="**/*.java"/>
   <exclude name="**/*Stub*"/>
</fileset>

文件集选择源文件夹中的所有 .java 文件,但那些包含单词“Stub”的文件除外。对文件集应用区分大小写的过滤器,这意味着不会从文件集排除名为 Samplestub.java 的文件。

The fileset selects all .java files in the source folder except those contain the word 'Stub'.The case-sensitive filter is applied to the fileset, which means a file with the name Samplestub.java will not be excluded from the fileset.

Pattern set

模式集是一种模式,允许根据某些模式轻松过滤文件或文件夹。可以使用以下元字符创建模式:

A pattern set is a pattern that allows to filter files or folders easily based on certain patterns. The patterns can be created using the following meta characters −

  1. ? - Matches one character only.

  2. - Matches zero or many characters.

  3. ** - Matches zero or many directories recursively.

以下示例描述了模式集的用法。

The following example depicts the usage of a pattern set.

<patternset id="java.files.without.stubs">
   <include name="src/**/*.java"/>
   <exclude name="src/**/*Stub*"/>
</patternset>

然后可以将模式集与文件集一起重新使用,如下所示:

The patternset can then be reused with a fileset as follows −

<fileset dir="${src}" casesensitive="yes">
   <patternset refid="java.files.without.stubs"/>
</fileset>

File list

文件列表数据类型与文件集类似,但存在以下差异:

The filelist data type is similar to the file set except the following differences −

  1. It contains explicitly named lists of files and it does not support wild cards.

  2. This data type can be applied for existing or non-existing files.

让我们看一下文件列表数据类型的以下示例。在此处,属性 webapp.src.folder 指向项目的 Web 应用程序源文件夹。

Let us see the following example of the filelist data type. Here, the attribute webapp.src.folder points to the web application source folder of the project.

<filelist id="config.files" dir="${webapp.src.folder}">
   <file name="applicationConfig.xml"/>
   <file name="faces-config.xml"/>
   <file name="web.xml"/>
   <file name="portlet.xml"/>
</filelist>

Filter set

通过将 filterset 数据类型与 copy 任务一起使用,可以使用替换值替换与模式匹配的所有文件中特定的文本。

By using a filterset data type along with the copy task, you can replace certain text in all the files that matches the pattern with a replacement value.

一个常见的例子是在发布说明文件中追加版本号,如下面的代码所示。

A common example is to append the version number to the release notes file, as shown in the following code.

<copy todir="${output.dir}">
   <fileset dir="${releasenotes.dir}" includes="**/*.txt"/>
   <filterset>
      <filter token="VERSION" value="${current.version}"/>
   </filterset>
</copy>

在上面提到的代码中:

In the above mentioned code −

  1. The attribute output.dir points to the output folder of the project.

  2. The attribute releasenotes.dir points to the release notes folder of the project.

  3. The attribute current.version points to the current version folder of the project.

  4. The copy task, as the name suggests, is used to copy files from one location to another.

Path

path 数据类型通常用于表示类路径。路径中的条目使用分号或冒号分隔。但是,这些字符会在运行时被执行系统的路径分隔符字符替换。

The path data type is commonly used to represent a class-path. Entries in the path are separated using semicolons or colons. However, these characters are replaced at the runtime by the executing system’s path separator character.

类路径被设置为项目中 jar 文件和类的列表,如下面的示例所示。

The classpath is set to the list of jar files and classes in the project, as shown in the example below.

<path id="build.classpath.jar">
   <pathelement path="${env.J2EE_HOME}/${j2ee.jar}"/>
   <fileset dir="lib">
      <include name="**/*.jar"/>
   </fileset>
</path>

在上面给出的代码中:

In the code given above −

  1. The attribute env.J2EE_HOME points to the environment variable J2EE_HOME.

  2. The attribute j2ee.jar points to the name of the J2EE jar file in the J2EE base folder.