Coding With Fun
Home Docker Django Node.js Articles FAQ

Ant data type


May 25, 2021 Apache Ant


Table of contents


Ant data type

Ant provides some predefined data types. Don't confuse the term "data types" with the data types that are available in the programming language, but think of them as a set of services that are already configured in the product.

The following data types are provided by Apache Ant.

The file set

The data type of the file set represents a collection of files. It is used as a filter to include or remove files that match a pattern.

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

The file set selects all the .java in the source folder, except those that contain the word 'Stub'. Case-sensitive filters are applied to the file set, which means that files .java Samplestub will not be excluded from the file set.

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

The pattern collection

A pattern collection refers to a pattern that makes it easy to filter files or folders. Patterns can be created using the meta-characters described below.

  • ? -Only one character is matched
  • - Match zero or more characters
  • - Recursively matches zero or more directories

The following example demonstrates the use of pattern collections.

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

The pattern collection can be reused with a file set similar to the following:

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

The list of files

File list data types are similar to file sets, except for the following:

  • The file list contains a list of explicitly named files, and it does not support wildcards.
  • File list data types can be applied to existing or existing files.

Let's look at an example of a file list data type below. In this example, the property webapp.src.folder points to the source folder of the web app in 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>

The collection of filters

Using a filter collection data type and copy task, you can replace some text that matches the pattern with a replacement value in all files.

A common example is an appended version number to a released document with the following code:

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

In this code:

  • The property output.dir points to the output folder of the project.
  • The property releasenotes.dir points to the project's release notes folder.
  • The property current.version points to the current version of the project folder.
  • Copy tasks, as the name implies, are used to copy files from one address to another.

Path

Path data types are often used to represent a class path. T he paths are separated by a sign or colon. However, these characters are replaced at runtime with path separators that execute the system.

The class path is set to a list of jar files and class files in the project, as shown in the following example:

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

In this code:

  • Property env. J2EE_HOME points to the environment variable J2EE_HOME .
  • The property j2ee .jar to a file named J2EE jar under the J2EE base folder.