我真的很喜欢 Ant 的“同步”任务,但我需要做的是根据目标文件的内容是否与源文件的内容匹配,将文件从源文件夹复制到目标文件夹,而不是检查文件修改日期(这是“同步”目前所做的)。有什么办法可以做到这一点吗?我注意到有一个用于文件内容的 Ant 比较器,以及一个可能会派上用场的“校验和”任务。
谢谢!
最佳答案
对于遇到此问题并需要代码的任何其他人,这里有一项任务是根据内容而不是时间戳将一个位置与另一个位置同步,它使用 修改 选择器而不是 不同 另一个答案中的选择器可以更好地控制文件差异的计算方式:
<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>
请注意,这会使 bar mirror foo (sync A->B),如果您想要双向同步,您可以用 B->A 中的副本替换删除,并提供一个 concat 任务来处理两个文件中对同一文件的更改地点。
关于ant - 如何使用 Ant 的 'sync' 任务,但基于文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5940918/