Sunday, July 12, 2015

Ant Data Types

As users of Ant build systems, we deal mostly with targets ($ant target). Targets, use multiple tasks(or actions) to achieve the goal. Echoing something to console, making directory, compiling source file are some of the commonly occurring tasks in Ant build. Task requires some inputs like a list of files, list of jars, known as datatypes in Ant world. 

Ant datatype store piece of information during the build, for example, list of files, to be created, compiled, deleted etc.  The datatypes can be declared inside a task or can be declared outside and then passed to task as a parameter. The second approach allows you to share the datatype in multiple tasks with the reference.  To stress on the concepts, I have abstracted some of the datatypes into simple targets:

File Set

Fileset datatype can abstract list of files to be compiled, copied, detected, tested etc. This datatype basically refers to set of files contained in a given directory (including files in subdirectories). Let's take a simple example of copying files from one directory to another directory:

//build.xml

<?xml version="1.0"?>
<project name="AntDemo">
 <fileset id="source.fileset" dir="src"/>

 <target name="copy1">
  <copy todir="backup1">
    <fileset refid="source.fileset"/>
  </copy>
 </target>
 
 <target name="copy2">
  <copy todir="backup2">
    <fileset dir="src"/>
  </copy>
 </target>
 
</project>

So, there are two approaches to copy files. In the first approach (copy1), we have declared a reference to the list of all Java files inside src directory. This is helpful if you want to do this activity multiple times. The second approach (copy2) is more traditional and provides all details inside the same element. For the first approach to work make sure that fileset was defined earlier.

$ant copy1
$ant copy2

Both targets perform the same operation. i.e. copy all files from src directory and copy them over to the backup directory.

Pattern Set

In above example, both targets will copy all files from the src directory. Now, what if you want to copy, say, only java files. Pattern set filters files from the given directory.

//build.xml

<?xml version="1.0"?>
<project name="AntDemo">
 <target name="copyJava">
  <delete dir="backup"/>
  <copy todir="backup">
    <fileset dir="src" includes="**/*.java"/>
  </copy>
 </target>
 
 <target name="copyJava2">
  <delete dir="backup"/>
  <copy todir="backup">
    <fileset dir="src">
     <include name="**/*.java"/>
    </fileset>
  </copy>
 </target>
 
 <patternset id="java.files.only">
     <include name="**/*.java"/>
     <exclude name="**/*.js"/>
 </patternset>
 
 <target name="copyJavaOnly">
  <delete dir="backup"/>
  <copy todir="backup">
    <fileset dir="src">
      <patternset refid="java.files.only"/>
    </fileset>
  </copy>
 </target> 
</project>

Above build.xml shows multiple ways you can apply filter to fileset.

**/*.java - matches all the java files in directory and sub-directories.
*.java - matches only java files in the given directory

File List

This data type is similar to fileset and it's useful when you need to order set of files. Also filelist can be applied on non-existent files.  Shown below a task which creates files.

 <touch>
  <filelist dir="." files="1.txt,2.txt">
   <file name="web.xml" />
   <file name="*.txt" />
  </filelist>
 </touch>

*.txt will just create a single file with name, *.txt.


Filter Set

During the build process, we might need to dynamically fill in values of the file with today's date or build number. Ant has a specific data type of handle this scenerio, filterset.

Below task copies, version.txt from build directory to dist directory and replaces the token @DATE with today's date.

 <copy file="${build.dir}/version.txt" toFile="${dist.dir}/version.txt">
  <filterset>
   <filter token="DATE" value="${TODAY}" />
  </filterset>
 </copy>

More examples, here.


Path

Path data type in Ant is used to represent Java CLASSPATH or the PATH environment variable of Unix/Windows systems. It's an ordered list of elements where each element can be either a file or directory.  We can separate individual elements with either color or semicolon (: or ;). Also for directories, we can choose either Unix style (forward slash, /) or Windows style (backward slash, \). Ant handles it properly depending on the type of system.

If path structure consists of a single path or location or with multiple files:
<path location="lib/junit.jar" />

<path>
 <pathelement path="build/classes;lib/junit.jar" />
</path>



Can be used below approach if path consists of set of files:
<path>
  <fileset dir="lib">
    <include name="*.jar"/>
  </fileset>
</path>


---
keep coding !!!


No comments:

Post a Comment