Sync Task - Anyway to preserve a file if it exists in target?

(Julian Hall) #1

I want to sync the contents from an zip archive to a location.

There are certain files that are in the archive that I want to keep in the target if they already exists. Essentially the ‘preserveInTarget’ option that the ant.sync provides.

This example I want to deploy the contents of to /tmp/testdestination/ but preserve the file ‘’ if it exists.

tasks testSync() {
    into( '/tmp/testdestination/' )
     from ( zipTree('')) {
        exclude { currFile ->
            ('' == && (new File(destinationDir,currFile.path).exists())

This seems to copy the file on the first invocation. But then seems to delete it on the next, adds it back again on the next after that… and so on. Any help on what I’m trying to accomplish would be greatful.


(Peter Niederwieser) #2

Gradle’s ‘Sync’ task doesn’t currently support this. The easiest way out is to use the Ant task.

(Julian Hall) #3

Ah thanks, i’ll stop banging my head against my desk now.

One questions though - with the ant.sync - I believe it uses the file time-stamp as a decision on moving the file or not.

From what I noticed the Gradle Sync task uses some sort of hash. Is that correct?

(Peter Niederwieser) #4

The current implementation of the ‘Sync’ task is very simple. It either copies nothing (if the task is up-to-date*) or everything (if the task is not up-to-date). In the latter case, it also deletes everything from the target that wasn’t copied.

(*) Up-to-date checks are a generic feature leveraging both hashing and timestamps.

(Julian Hall) #5

Hi Peter - Just curious how you would get around with without using Ant’s sync?

I was attempting to do it before but ran into issues:

(Davide Cavestro) #6

Is there any chance to add this feature to the Sync Task? Having something like a

preventDelete ‘myfile.txt’ would come really handy. The semantic would be

  1. check pre-existing files that weren’t copied 2. if they match the “delete prevention” pattern leave them alone 3. otherwise proceed with the deletion