Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle copy without overwrite

Tags:

gradle

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

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 avoid to specify all the files using exclude 'file/file*'.

It looks like that duplicatesStrategy= DuplicatesStrategy.EXCLUDE doesn't work. I read about an issue on gradle 0.9 but I'm using Gradle 2.1.

Is this problem still there?

Or am I misunderstanding how this task should be used properly?

Thanks

like image 949
carlitos081 Avatar asked Dec 04 '14 11:12

carlitos081


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")
    }
}
like image 152
ernirulez Avatar answered Oct 22 '22 04:10

ernirulez


A further refinement of BugOrFeature's answer. It's using simple strings for the from and into parameters, uses the CopySpec's destinationDir property to resolve the destination file's relative path to a File:

task ensureLocalTestProperties(type: Copy) {
    from zipTree('/app-war/app.war')
    into 'WebContent'
    eachFile {
        if (it.relativePath.getFile(destinationDir).exists()) {
            it.exclude()
        }
    }
 }
like image 21
SurlyDre Avatar answered Oct 22 '22 05:10

SurlyDre


Sample 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()
        }
    }
}
like image 26
BugOrFeature Avatar answered Oct 22 '22 05:10

BugOrFeature