Search

Dark theme | Light theme

October 5, 2020

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 other 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 replacement. As extra safeguard we can let the tsak 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:

// 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:

// 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.