Gradle Sync task, sync into folder but ignore specified directories in destination

StackOverflow https://stackoverflow.com/questions/23669756

  •  23-07-2023
  •  | 
  •  

Question

I want to synchronize a folder on my drive with another folder that contains a folder named 'logs' that i want to keep. confused? here's a diagram:

C:\
|-- mydir/ # sync this folder
|   `-- someotherfiles.txt
`-- anotherDir/ # into this folder
    |-- logs/ # but if this exists, leave it there
    `-- someotherfiles.txt

Is this possible using the sync task? I can't seem to configure it properly, my latest attempt might allude you to my scenario so here it is (not working):

task syncDevDeployFolder(type: Sync, group: 'dev') {
    from currentDeliverablesDir
    destinationDir = file(project.properties['dev.deployment.dir'])
    into (project.properties['dev.deployment.dir']) {
        exclude "logs"
    }
}
Was it helpful?

Solution 2

Is this possible using the sync task?

No, the Sync task doesn't currently support this.

OTHER TIPS

At the time of writing, the current version of Gradle is 3.3 and since version 3.1 there is an incubating feature called 'preserve' which can be used to achieve what you want:

See the example from the documentation:

// You can preserve output that already exists in the
// destination directory. Files matching the preserve
// filter will not be deleted.
task sync(type: Sync) {
    from 'source'
    into 'dest'
    preserve {
        include 'extraDir/**'
        include 'dir1/**'
        exclude 'dir1/extra.txt'
    }
}

So in your case you could specify it like this:

preserve {
    include 'logs/**'
}

You can fallback to ant's sync task. ant object is available to all gradle build scripts anyways

task antSync << {
    ant.sync(todir:"dest/"){
        ant.fileset(dir: "source/")
        ant.preserveintarget(includes: "logs/")
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top