ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Peter Reilly" <peter.kitt.rei...@gmail.com>
Subject Re: Implementing a loop in ANT
Date Thu, 08 Jun 2006 08:10:49 GMT
Here is a example (beanshell rocks!):

<project name="loop" xmlns:ac="antlib:net.sf.antcontrib">
    <script language="beanshell">
        import java.util.Iterator;
        public class Loop implements Iterator {
            private int begin = 0;
            private int end   = 10;
            private int curr = 0;
            public void setBegin(int b) {
               begin = b;
               curr = b;
            }
            public void setEnd(int e) {
               end = e;
            }
            public Iterator iterator() {
                return this;
            }
            public boolean hasNext() {
                return curr &lt; end;
            }
            public Object next() {
                Integer ret = new Integer(curr);
                curr = curr + 1;
                return ret;
            }
            public void remove() {
            }
        }
        project.addDataTypeDefinition("loop", Loop.class);
    </script>

    <ac:for param="val">
        <loop end="5"/>
        <sequential>
            <echo message = "val = @{val}"/>
        </sequential>
    </ac:for>
</project>

This outputs:
C:\work\test>ant -f for.xml
UserLib is C:\Documents and Settings\reilly\.ant\lib
Buildfile: for.xml
     [echo] val = 0
     [echo] val = 1
     [echo] val = 2
     [echo] val = 3
     [echo] val = 4

BUILD SUCCESSFUL

Peter
On 6/7/06, Dominique Devienne <ddevienne@gmail.com> wrote:
>
> Ant-Contrib's <for> accepts any type which exposes an iterator()
> method I believe, so you'd have to write a Java task or <scriptdef> to
> create such an <integer-range> type.
>
> You could also try a recursive <macro> or <antcall> (with proper
> stopping condition ;-) that writes the numbers to a file, and then
> <loadfile> it.
>
> Using a little <script> to generate the series in a property would
> probably be easier though. --DD
>
> On 6/7/06, Guru Balse <guru.balse@oracle.com> wrote:
> > I am sure this question has been asked before, and I could not see any
> > reasonable answer in the archives.  How can I implement a loop in ANT
> > without using scripts?  For example, if I want to call a certain target
> > N times, how can I do it?
> >
> > Using ant-contrib is OK.  Of course, something like <for
> > list="1,2,3,4,5,6,...,N"> would work but I want N to be a property that
> > can be specified in the command line.
> >
> > Thanks in advance for your suggestions.
> >
> >  - Guru Balse
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

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