2012-01-30 61 views
3

當我在macrodef中的groovy任務中使用ant macrodef屬性時,我無法運行移動文件的代碼。在Groovy中訪問file.eachFileMatch()中的模式變量的ant屬性值

<macrodef name="dirmove"> 
      <attribute name="todir" /> 
      <attribute name="fromdir" /> 
      <attribute name="includes" default="*" /> 
      <sequential> 
       <var name="todir" value="@{todir}" /> 
       <var name="fromdir" value="@{fromdir}" /> 
       <var name="includes" value="@{includes}" /> 
       <groovy> 
       File dir1 = new File(properties.'fromdir'); 
       File dir2 = new File(properties.'todir'); 
       def pattern = properties.get('includes') 
       println pattern; 
       dir1.eachFileMatch ~/pattern/, { 
        f-> 
        boolean fileMoved = f.renameTo(new File(dir2, f.getName())); 
        //assert f.name == '1.txt' //**because File object is immutable, so I am just checking for the existing of previous file name. It is still there. 
        println fileMoved; 
       } 
      </groovy> 
     </sequential> 
    </macrodef> 

此代碼正確打印了來自屬性值的模式值。但eachFileMatch功能不拿起規範

回答

0
  dir1.eachFileMatch ~/pattern/, { 

應該

  dir1.eachFileMatch ~/${pattern}/, { 

由於圖案是字符串變量,所以你需要將它添加到正則表達式本身。

以前,你剛纔搜索所有文件名爲pattern

+0

感謝@ TIM-yates..this解決的問題。 – user1173339 2012-01-31 05:19:08

+0

@ user1173339好消息! :-)你能將這個答案標記爲[accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)嗎? – 2012-01-31 09:53:53