DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Introduce a New API Quickly Using Spring Boot and Gradle
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Building AI Applications With Java and Gradle
  • Understanding Dependencies...Visually!

Trending

  • FastAPI + Django in Production: Lessons From a Hybrid Stack
  • Why Enterprise AI Agents Fail: A Runtime Data Governance Pattern for Reliable Answers
  • Build Your Own Local AI QA Engineer With Docker, Ollama, LibreChat, and Playwright MCP
  • Designing Secure REST APIs With Spring Boot

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.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Oct. 22, 20 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
6.0K Views

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

Groovy
xxxxxxxxxx
1
21
 
1
// Register new task replaceZip of type org.gradle.api.tasks.bundling.Zip.
2
tasks.register("replaceZip", Zip) {
3
    archiveBaseName = "new-sample"
4
    destinationDirectory = file("${buildDir}/archives")
5

           
6
    // Include the content of the original archive.
7
    from(zipTree("${buildDir}/archives/sample.zip")) {
8
        // But leave out the file we want to replace.
9
        exclude("README")
10
    }
11

           
12
    // Add files with same name to replace.
13
    from("src/new-archive") {
14
        include("README")
15
    }
16

           
17
    // As archives allow duplicate file names we want to fail 
18
    // the build when that happens, because we want to replace
19
    // an existing file.
20
    duplicatesStrategy = "FAIL"
21
}


And the same task but now with Kotlin DSL:

Kotlin
xxxxxxxxxx
1
21
 
1
// Register new task replaceZip of type org.gradle.api.tasks.bundling.Zip.
2
tasks.register<Zip>("replaceZip") {
3
    archiveBaseName.set("new-sample")
4
    destinationDirectory.set(project.layout.buildDirectory.dir("archives"))
5

           
6
    // Include the content of the original archive.
7
    from(project.zipTree("$buildDir/archives/sample.zip")) {
8
        // But leave out the file we want to replace.
9
        exclude("README")
10
    }
11

           
12
    // Add files with same name to replace.
13
    from(file("src/new-archive")) {
14
        include("README")
15
    }
16

           
17
    // As archives allow duplicate file names we want to fail 
18
    // the build when that happens, because we want to replace
19
    // an existing file.
20
    duplicatesStrategy = DuplicatesStrategy.FAIL
21
}


Written with Gradle 6.6.1.

Archive file Gradle

Published at DZone with permission of Hubert Klein Ikkink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introduce a New API Quickly Using Spring Boot and Gradle
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Building AI Applications With Java and Gradle
  • Understanding Dependencies...Visually!

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook