Gradle copy task - how to skip copying files if exists at destination

I’m using gradle 7.5.1. Is there a way to tell overwrite=false (like ant) on gradly copy task so that it won’t overwrite the files if exists at destination directory… after googling, i found below solution but it is 4yrs old solution… is it the same even now also or do we have any simple flag we can set on copy task in latest gradle ?

task myCopy(type: Copy) {

    def destination = project.file("WebContent")
    from rootDir.getPath() + "/tmp"
    into destination
    eachFile {
        if (it.getRelativePath().getFile(destination).exists()) {
            it.exclude()
        }
    }
}

@Vampire can you share your thoughts on this? Im guessing there must be simple way to do it… but couldn’t find any documentation on it…

You really shouldn’t ping people just because you want an answer and think someone can give it, that’s pretty rude.

But no, I don’t think there is any different way besides using the Ant task, at least nothing I’m aware of.

Besides that, the example is not using task configuration avoidance.

Apologies for tagging you… i wont do again…

This is the way i implemented it… it works but not sure whether its right approach or not…

tasks.register('copyConfAssets',Copy){
	/** copy conf files */
	def destn="$projectDir/conf"
	from "$apiContainerBaseDir/conf"
	into "$destn" 
	eachFile {
		if (it.getRelativePath().getFile(project.file("$destn")).exists()) {
			println "$it.path exists at destination, skipping it"
			it.exclude()
		}
	}
	doLast {
		println "api-container conf assets copied"
	}
}
1 Like