4

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.

5
  • I need to copy (file.txt dir to file2.txt dir) e.g abcBIN files dir to apps/xxx/yyy/bin/abc and so on. One to one mapping. Aug 4, 2012 at 14:36
  • 2
    If you really want to do that, you need to put the second <for> inside <sequential>, and also, you need to put the <echo>.....</copy> part inside a <sequential> inside the second <for>.
    – Dante WWWW
    Aug 6, 2012 at 6:19
  • coolcfan, if i use the logic you mentioned. It wont work for one to one mapping. e.g. one line from src then one line from dest dir Aug 7, 2012 at 23:59
  • @ShwetaChandrakar You could post your solution so that others could benefit from it.
    – FailedDev
    Aug 15, 2012 at 20:53
  • @ShwetaChandrakar I need to know this solution .. thanks Aug 22, 2012 at 9:07

1 Answer 1

2

@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>
6
  • Thanks; you may want to mark this as the answer so others may benefit. Aug 24, 2012 at 2:46
  • though this works.. I'd call it a hack. @FailedDev whatsay? Can you think of a cleaner solution. Aug 24, 2012 at 2:48
  • @PulakAgrawal: well, this is not a hack. This is a in built commmand i used in ANT. The actual text file was a single file. I did split it for my oown convinience. Then I thought of using the original. Aug 24, 2012 at 11:28
  • @PulakAgrawal: If you still think it is not useful then I am happy to remove it from the forum. This was the reason I didn't post the answer before. I did it when you asked me to do so. I am new in ANT. I may have not given the best. Aug 24, 2012 at 11:31
  • :) Please don't remove anything. This is a valid answer as it does the job. And this will be useful for many. At least I have kept it in my favorite list. And with my experience I can tell you are in top 10% of ANT developers already so no worries. If you do not like the word "hack", ok, but I still maintain its not a clean solution as we are processing from OS, then to a file (which contains output of LS ??) and then going back to OS to do the copy. If I could come up with a cleaner one, I would have answered myself :) Aug 27, 2012 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.