How to use Ant "sync" task, but based on file content

I really like Ant sync 'task, but I need to do this in order to copy files from the source folder to the destination folder based on whether the contents of the target file match the contents of the source file, rather than checking the file modification date (which means “synchronization” in present). Is there any way to do this? I noticed that there is an Ant comparator for the contents of the file, as well as a “checksum” task that may come in handy.

Thanks!

+7
source share
3 answers

I was able to accomplish this with the Ant 'copy' task, which had a set of files with a different selector (see http://ant.apache.org/manual/Types/selectors.html#differentselect ). This is powerful stuff.

+2
source

For anyone else who accesses this and needs code, here is the task of synchronizing one location with another based on the content, not the timestamp, it uses the changed selector and not the other in another answer to give more control over the difference of the files:

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

Note that the foo bar-mirror does this (synchronization A-> B), if you want bi-directional synchronization, you can replace the deletion with a copy from B-> A and provide a concatenated task to handle changes to the same file in both places.

+5
source

Thanks for this task!

However, the selectCopyFiles file set was incorrect. I have another solution for selectDeleteFiles.

Here is the new macrodef code:

 <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> 
+1
source

All Articles