Gradle Goodness: Replace Files In Archives
In this article, take a look at an example of a task to replace a README file in an archive sample.zip using Groovy and Kotlin DSL.
Join the DZone community and get the full member experience.Join For Free
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
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.
The following code shows an example of a task to replace a
README file in an archive
sample.zip using Groovy and Kotlin DSL. First the Groovy DSL:
And the same task but now with Kotlin DSL:
Written with Gradle 6.6.1.
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.