Gradle copy without overwriting

Is there a way to avoid overwriting files when using the type:Copy task?

This is my task:

 task unpack1(type:Copy) { duplicatesStrategy= DuplicatesStrategy.EXCLUDE delete(rootDir.getPath()+"/tmp") from zipTree(rootDir.getPath()+"/app-war/app.war") into rootDir.getPath()+"/tmp" duplicatesStrategy= DuplicatesStrategy.EXCLUDE from rootDir.getPath()+"/tmp" into "WebContent" } 

I want to not specify all files using exclude 'file / file *'.

It seems that duplicatesStrategy= DuplicatesStrategy.EXCLUDE not working. I read about the problem on gradle 0.9, but I am using gradle 2.1.

Is this problem still there?

Or do I not understand how this task should be used properly?

thanks

+5
source share
3 answers

You can always check first if the file exists in the destination directory:

 task copyFileIfNotExists << { if (!file('destination/directory/myFile').exists()) copy { from("source/directory") into("destination/directory") include("myFile") } } 
+2
source

Further refinement of BugOrFeature's answer. It uses simple strings for the from and in parameters, uses the CopySpec destinationDir property to resolve the relative path of the destination file to the file:

 task ensureLocalTestProperties(type: Copy) { from zipTree('/app-war/app.war') into 'WebContent' eachFile { if (it.relativePath.getFile(destinationDir).exists()) { it.exclude() } } } 
+1
source

An example based on Peter's comment:

 task unpack1(type: Copy) { def destination = project.file("WebContent") from rootDir.getPath() + "/tmp" into destination eachFile { if (it.getRelativePath().getFile(destination).exists()) { it.exclude() } } } 
0
source

Source: https://habr.com/ru/post/1208349/


All Articles