Search code examples
gradlefile-permissions

Setting file permissions dynamically in a gradle Copy task


I'm trying to get around a problem where a dependency in my build is a zip file that contains some read only files. When I extract that zip as part of my build I end out with read only files in a staging folder and they prevent the task running in the future since they cannot be overwritten.

Until there's a way to force overwrite in a gradle copy task I've been trying to find a way to change the file mode of the read-only files in a way that doesn't remove the execute bit from those files that need it.

I've come up with this:

task stageZip(type: Copy) {
  from({ zipTree(zipFile) })
  into stagingFolder

  eachFile {
    println "${it.name}, oldMode: ${Integer.toOctalString(it.mode)}, newMode: ${Integer.toOctalString(it.mode | 0200)}"
    fileMode it.mode | 0200
  }
}

But this doesn't work. If I comment out the fileMode line then the println correctly lists the old and new file modes with the write bit enabled for all files. If I leave the code as is, then all the files in the zip get extracted with the newMode of the first file.

This doesn't seem like this is an unreasonable thing to try and do, but I'm obviously doing something wrong. Any suggestions?


Solution

  • Based on this thread, consider the Sync task. Specifically:

    task stageZip(type: Sync) {
        from zipTree('data/data.zip')
        into 'staging'
        fileMode 0644
    }
    

    I've put a working example (as I understand the question) here.