tags:

views:

170

answers:

1

Let's say I copy some files with ant, from a network 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.

+1  A: 

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.

Peter Lang
Yeah ... sorry about that. I've edited it to make it a bit clearer. I want to synchronize the two folders, in order to always have the version of the folder that exists on the server.
Geo
Ok, edited my answer. Is this what you want or am I missing anything?
Peter Lang
If I add the `overwrite` attribute, all the files get copied to the local folder, even though they're the same. I would like to avoid unnecessary copy.
Geo
That's why you do **not** want to set it :) Shouldn't the default behavior be sufficient for you?
Peter Lang
`preservelastmodified` doesn't work. The timestamp on the local files will be newer than the timestamp of the files located on the server.
Geo
I'm afraid I still don't understand. As I said, do not set `overwrite`. If timestamps on local files are newer, then they will not be overwritten - which is what you want, right?
Peter Lang
No... it's exactly the opposite. If the timestamps on my files are newer, I want them to be overwritten. And if the timestamps are the same, I'd like them not to be overwritten.
Geo
Ah, sorry. Now I understood your problem. Maybe my updated answer can help...
Peter Lang