2012-08-04 269 views
4

我有兩個txt文件:File1.txt - 包含src目錄列表;和File2.txt - 包含dest目錄的列表。我需要使用從src目錄到目錄目錄的循環進行復制。Ant嵌套循環

FILE1.TXT(SVN可怕的結構)

abcBIN 
abcBIN/fdPro 
...so on 

FILE2.TXT(LINUX結構)

​​

我需要複製的文件abcBIN DIR到應用程序/ XXX/YYY /斌/ abc等。一對一映射。

<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> 

雖然它不工作。

我得到一個錯誤:

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

我不能使用UNIX shell或Perl。它必須在Ant中完成。

如果您對Ant中的嵌套循環有更好的瞭解,請告知我們。

+0

我需要複製(file.txt的DIR到DIR FILE2.TXT)e.g abcBIN文件目錄到應用程序/ XXX/YYY /斌/ ABC等。一對一映射。 – 2012-08-04 14:36:16

+0

什麼是你的java版本? – FailedDev 2012-08-04 20:11:21

+2

如果你真的想這樣做,你需要把第二個''放在''裏面,並且你還需要把' .....'部分放在第二個''' '。 – coolcfan 2012-08-06 06:19:37

回答

2

@PulakAgrawal:我使用冒號作爲行分隔符組合兩個文本文件合併成一個和魔法開始:)

例如SRC路徑:DEST路徑

 <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> 
+0

謝謝;您可能希望將此標記爲答案,以便其他人可能受益。 – 2012-08-24 02:46:15

+0

雖然這工作..我會稱它爲黑客。 @FailedDev whatsay?你能想到一個更清潔的解決方案嗎? – 2012-08-24 02:48:42

+0

@PulakAgrawal:好吧,這不是黑客。這是我在ANT中使用的一個內置命令。實際的文本文件是單個文件。爲了我的方便,我做了分裂。然後我想到了使用原版。 – 2012-08-24 11:28:18