ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alan Peng" <ap...@atg.com>
Subject [SUBMIT] Iterate Task
Date Thu, 08 Nov 2001 22:54:08 GMT
I would like to submit a task that allows you to repeatedly call a target
passing different values from a list.

Ant does not support iteration or loop mechanisms that allows you to repeat
certain processes. Without such a mechanism, repeat processes have to be
hard coded, one entry in the build script per occurrence. This is less than
desirable because as it requires the modification of the build script each
time the project changes.

An iteration Java task has been developed which better supports the repeat
processes in the build.

·               org.apache.tools.ant.taskdefs.Iterate.java

Note: We had to use the standard Ant package names because the custom task
requires access to certain protected functions in classes in the Ant
package.

This new task repeatedly calls a target passing different values from a
delimited string.

For example:
<project name="Ant_Extensions" default="main" basedir=".">
   <taskdef name="iterate" classname="org.apache.tools.ant.taskdefs.myTest">

   <target name="main">
     <iterate target="hello" itemList="1,3,5" id="iterate.value" >
       </iterate>
   </target>

   <target name="hello">
     <echo message="${iterate.value}" />
   </target>
 </project>

The above example will call the hello target three times, each time passing
a value from the item list. In this case the hello target will echo 1, then
3 and then 5.

A more useful example is the ability to compile multiple source directories
into multiple jar files.

For example:
 …
 <target name="build" depends="init">

   <!-- iterate through the ${build.modules} variable, compiling each module
specified -->
   <iterate target="javac" itemList="myModule,myModule/mySubModule"
id="iterate.module"/>
 </target>

 <target name="javac" depends="checkSource, cleanBuild, prepareBuild"
if="compile.source.exist">

   <javac  srcdir="${iterate.module}/src" destdir="${iterate.module}/build">
     <include name="**\/*.java"/>
   </javac>

   <!-- create a jar file for each module-->
   <mkdir dir="${iterate.module}/lib"/>

   <jar jarfile="${iterate.module}/lib/classes.jar">
     <fileset dir="${iterate.module}/build"/>
   </jar>
 </target>
 …

 The above example does the following:

·               Compiles the myModule/src directory into
myModule/lib/classes.jar
·               Compiles the myModule/mySubModule/src directory into
myModule/mySubModule/lib/classes.jar

I have included the source, classes and a example build script that calls
the new task.

Cheers,
Alan

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message