2011-05-09 38 views
7

我真的很喜歡Ant'sync'任務,但我需要做的是將文件從源文件夾複製到目標文件夾根據是否目標文件的內容與源文件的內容匹配,而不是檢查文件修改日期(這是'同步'當前的操作)。有什麼辦法可以做到這一點?我注意到有一個用於文件內容的Ant比較器,以及一個可能派上用場的'校驗和'任務。如何使用Ant的'同步'任務,但基於文件內容

謝謝!

+0

曾經考慮調用rsync的執行同步操作? – ewh 2011-05-20 05:02:13

+1

感謝您的想法。如果可能,我正在尋找一種基於螞蟻的解決方案。 – 2011-05-20 20:02:12

回答

5

爲別人擊中這一點,需要的代碼,這裏是另一種基於內容而不是時間戳同步一個位置上的任務,它採用了修改選擇,而不是不同選擇在其他答案給了文件的差異是如何計算的更多的控制權:

<project name="Demo" default="newSync"> 
    <description> 
    Sync from ${foo} to ${bar} 
    </description> 

    <macrodef name="syncContents"> 
    <attribute name="from"/> 
    <attribute name="to"/> 
    <sequential> 
     <fileset id="selectCopyFiles" dir="@{from}"> 
     <modified algorithm="hashvalue"/> 
     </fileset> 
     <fileset id="selectDeleteFiles" dir="@{to}"> 
     <not> 
      <present targetdir="@{from}"/> 
     </not> 
     </fileset> 

     <copy overwrite="true" todir="@{to}"> 
     <fileset refid="selectCopyFiles"/> 
     </copy> 
     <delete includeEmptyDirs="true"> 
     <fileset refid="selectDeleteFiles"/>    
     </delete> 
    </sequential> 
    </macrodef> 

    <target name="newSync"> 
    <syncContents from="${foo}" to="${bar}"/> 
    </target> 
</project> 

注意,這使得柱狀鏡富(同步A-> B),如果你想有一個雙向同步,你可以更換一個副本從B-刪除> A,並提供一個concat任務來處理對兩個位置中相同文件的更改。

1

謝謝你的任務!

但是,selectCopyFiles文件集不正確。我還爲selectDeleteFiles文件集提供了另一種解決方案。

這裏是macrodef的新代碼:

<macrodef name="syncContents"> 
    <attribute name="from"/> 
    <attribute name="to"/> 
    <sequential> 
     <echo>syncContents  : @{from} -> @{to}</echo> 
     <fileset id="selectCopyFiles" dir="@{from}"> 
      <different targetdir="@{to}" 
      ignoreFileTimes="true"/> 
     </fileset> 
     <fileset id="selectDeleteFiles" dir="@{to}"> 
      <present present="srconly" targetdir="@{from}"/> 
     </fileset> 

     <copy overwrite="true" todir="@{to}" preservelastmodified="true" verbose="true"> 
      <fileset refid="selectCopyFiles"/> 
     </copy> 
     <delete includeEmptyDirs="true" verbose="true"> 
      <fileset refid="selectDeleteFiles"/> 
     </delete> 
     <echo>End syncContents : @{from} -> @{to}</echo> 
    </sequential> 
</macrodef>