Building with Apache ANT part 2
In the previous post i have mentioned that how to set up ant and how to create a simple build.xml file. Here are more stuff to do with Ant.
Using separate property file
When the number of properties are handful, then it is ok to use all those properties in the same build file. But what about when we got a large number of properties and we have to use them again and again. The most suitable thing is to use a separate file which contains all the properties. What we need to do is , simply calling that file within our build.xml file.These different kind of properties can be used in different building environment such as testing, developing etc.
Let’s use a separate properties file and call it inside our XML file. This is build.properties file.
sitename = www.ant.apache.org
buildversion=3.3.2
This is the build.xml file
<?xml version="1.0"?>
<project name="myproject" default="info">
<property file="build.properties"/>
<target name="info">
<echo> ${buildversion} - ${sitename} </echo>
</target>
</project>
After running this, it will give the output as
3.3.2 - www.ant.apache.org.
Data Types in ANT
There are set of predefined data types in ANT.
File Set
This data type represents a collection of files.This data type is usually used to include or exclude the correspondent file.
For an example.
<fileset dir="${src}" casesensitive="yes">
<include name="**/*.java"/>
<exclude name="**/*Techno*"/>
</fileset>
In here, the fileset select all the java files in source folder except the files which contain “Techno”. But it is ok with the file named “techno”, because the case sensitive is on.
Pattern Set
Using this data type, we can easily filter up the files by considering a given pattern.There are special meta characters in ANT which are use to create patterns.
- ? — Matches one character only
- * — Matches zero or many characters
- ** — Matches zero or many directories recursively
I create a my own pattern set as follows.
<patternset id="java.files.without.techno">
<include name="src/**/*.java"/>
<exclude name="src/**/*Techno*"/>
</fileset>
This pattern id can be anything. Then we use this pattern set to filter files.
<fileset dir="${src}" casesensitive="yes">
<patternset refid="java.files.without.techno"/> </fileset>
Then the same out put can be taken as the previous example.
File List
This data type is also similar to File Set, but with some differences.File list can contain explicitly named files. And also this is not supporting for wild cards.Other difference is that file list data type can be applied to the files which are exist or not.
For an example:
<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>
So the File list data type is applied to the webapp.src.folder as the source folder.
Filter Set
We can use the filter set data set along with its copy task, to replace a given text in all files according to a custom pattern of replacement value.
<copy todir="${output.dir}">
<fileset dir="${releasenotes.dir}" includes="**/*.txt"/>
<filterset>
<filter token="VERSION" value="${current.version}"/>
</filterset>
</copy>
I used this XML to append the version number to the release file.
output.dir
is the destination file.
releasenotes.dir
is the source directory.
Path
Path data type is basically used to manipulate the class path. The entries inside the Path data type is separated by colon or a semicolon. Following, how we set the list of jar files and classes in the project to the class path.
<path id="build.classpath.jar">
<pathelement path="${env.J2EE_HOME}/${j2ee.jar}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
env.J2EE_HOME — J2EE home variable.
j2ee.jar — J2EE jar file in the J2EE base folder.