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 FreeSometimes, 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.
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:
xxxxxxxxxx
// Register new task replaceZip of type org.gradle.api.tasks.bundling.Zip.
tasks.register("replaceZip", Zip) {
archiveBaseName = "new-sample"
destinationDirectory = file("${buildDir}/archives")
// Include the content of the original archive.
from(zipTree("${buildDir}/archives/sample.zip")) {
// But leave out the file we want to replace.
exclude("README")
}
// Add files with same name to replace.
from("src/new-archive") {
include("README")
}
// As archives allow duplicate file names we want to fail
// the build when that happens, because we want to replace
// an existing file.
duplicatesStrategy = "FAIL"
}
And the same task but now with Kotlin DSL:
xxxxxxxxxx
// Register new task replaceZip of type org.gradle.api.tasks.bundling.Zip.
tasks.register<Zip>("replaceZip") {
archiveBaseName.set("new-sample")
destinationDirectory.set(project.layout.buildDirectory.dir("archives"))
// Include the content of the original archive.
from(project.zipTree("$buildDir/archives/sample.zip")) {
// But leave out the file we want to replace.
exclude("README")
}
// Add files with same name to replace.
from(file("src/new-archive")) {
include("README")
}
// As archives allow duplicate file names we want to fail
// the build when that happens, because we want to replace
// an existing file.
duplicatesStrategy = DuplicatesStrategy.FAIL
}
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.
Comments