开发者

How can I synchronize two folders using Ant?

开发者 https://www.devze.com 2022-12-22 07:56 出处:网络
Let\'s say I copy some files with ant, from a networ开发者_C百科k drive: <copy todir=\".\" verbose=\"true\">

Let's say I copy some files with ant, from a networ开发者_C百科k drive:

<copy todir="." verbose="true">
   <fileset dir="some_directory" includes="**/*"/>
</copy>

Let's say I test if the folder exists first.

<available file="${dir.local}" property="dir.exists"/>

If I have the folder on my computer, I would like to only copy the files that are modified. Is there any way of keeping up2date with the version that exists on the server?

EDIT: I know about the sync task. The thing is, if my local files are modified, sync does not copy them. Is there any way to go around that behaviour, or is there another task which can do this?

EDIT2: here's the code modified to according to Peter's suggestions:

<target name="copy">
  <echo>${dir.remote}</echo>
  <copy todir="${dir.local}" verbose="true" overwrite="true" 
  preservelastmodified="true">
     <fileset dir="${dir.remote}">
        <include name="**/*"/>
     </fileset>
  </copy>
</target>

This however copies all the files. It's not only replacing the modified ones.


Since Apache Ant 1.6 you can use the sync task for synchronize your folders. Example:

    <sync todir="site">
      <fileset dir="genereted-site" />
    </sync>

Visit http://ant.apache.org/manual/Tasks/sync.html for more information.


Edit: I might be wrong, but I don't think that such a task exists.

One way to do this would be to write your own Ant Task extending the Copy task and overriding copySingleFile which checks the timestamp:

if (forceOverwrite || !destFile.exists()
    || (file.lastModified() - granularity > destFile.lastModified())) {

Ant copy does not overwrite existing files unless the source is newer:

By default, files are only copied if the source file is newer than the destination file, or when the destination file does not exist. However, you can explicitly overwrite files with the overwrite attribute.

Use preservelastmodified to make sure that timestamps match.

0

精彩评论

暂无评论...
验证码 换一张
取 消