Gradle Goodness: Replace Files In Archives

Sometimes, we might need to replace one or more files in an existing archive file. The archive file could be a zip, jar, war, or another archive. Without Gradle, we would unpack the archive file, copy our new file into the destination directory of the unpacked archive, and archive the directory again. To achieve this with Gradle, we can simply create a single task of type Zip

To get the content of the original archive, we can use the project.zipTree method. We leave out the file we want to replace and define the new file as a replacement. As an extra safeguard, we can let the task fail if duplicate files are in the archive because of our replacement.