Search code examples
antcopyant-contrib

Ant Nested Loop


I have two txt files: File1.txt – contains list of src dir; and File2.txt – contains list of dest dir. I need to do the copy using a loop from src dir to dest dir.

File1.txt (SVN dire structure)

abcBIN
abcBIN/fdPro
...so on

File2.txt (LINUX structure)

apps/xxx/yyy/bin/abc
apps/xxx/yyy/bin/abc/fdpro
...so on

I need to copy the abcBIN files dir to apps/xxx/yyy/bin/abc and so on. One to one mapping.

<project xmlns:ac="antlib:net.sf.antcontrib">

<taskdef resource="net/sf/antcontrib/antcontrib.properties"> 
   <classpath>
      <pathelement location="path-to-ant-contrib.jar"/>
   </classpath>
</taskdef>

<loadfile property="file1" srcfile="File1.txt"/> 
<loadfile property="file2" srcfile="File2.txt"/>

<ac:for param="i" list="${file1}">
    <ac:for param="j" list="${file2}"> 
        <sequential>
           <echo>@{i}@{j}</echo>
           <echo>copying....</echo>

           <property name="src.dir" value="/home/name/svn_repo/dir" />
           <property name="dest.dir" value="/home/name/mapp" /> 
           <copy todir="${dest.dir}/@{j}">
              <fileset dir="${src.dir}/@{i}"> 
              </fileset>
           </copy>     
        </sequential>
     </ac:for>
</ac:for>

</project>

It is not working though.

I am getting an error:

ac:for doesn't support the nested "for" element

I can’t use UNIX shell or Perl. It has to be done in Ant.

Please let me know if you have any better idea about the nested loop in Ant.


Solution

  • @PulakAgrawal: I combined two text files into one using colon as a line separator and the magic began :)

    e.g. src path:dest path

         <loadfile property="allfiles" srcFile="mapping"/>
    
          <ac:for list="${allfiles}" param="line" delimiter="${line.separator}">
    
         <ac:sequential>
    
                <ac:propertyregex property="from" input="@{line}" regexp="(.*):(.*)" select="\1" override="true"/>
    
                <ac:propertyregex property="to" input="@{line}" regexp="(.*):(.*)" select="\2" override="true"/>
    
                <echo>Copying dir ${from} to ${to} ...</echo>
    
                <property name="src.dir" value="." /> <property name="dest.dir" value="." />
    
                <copy todir="${dest.dir}/${to}">     <fileset dir="${src.dir}/${from}">  </fileset> </copy>
    
         </ac:sequential>
    
         </ac:for>