How to iterate filenames inside zipfile/warfile without creating /tmp/expandedArchives


(Arnoud ) #1

Hi,

We have a multimodule project in which we implemented some safeguards by creating a verifyWar target to check the generated warfile. This check runs for all the projects with apply our war.gradle

[war.gradle]

/**
 * generic war configuration used by all modules which build wars
 *
 * add to your project using:
 *     apply from: "$rootProject.projectDir/war.gradle"
 */

apply plugin: 'war'

/* ... deleted irrelevant task definitions ... */

task verifyWar {
    def destFile = file("$buildDir/verifyWar.txt")
    dependsOn war
    inputs.file war.archivePath
    outputs.file destFile
    doLast {
        zipTree(war.archivePath).each { file ->
            assert ("some-forbidden.jar" != file.name)
            assert ("some-forbidden-resource.xml" != file.name)
        }
        ant.echo(file: destFile, message:"verified at " + new Date())
    }
}
war.finalizedBy verifyWar

What happens however is that every time verifyWar is run a .build/tmp/expandedArchives/myproject.war_{hash} is created.

Gradle doesn’t clean it up afterwards, so if a developer doesn’t run clean often enough the new war is unpacked and the expandedArchives directory grows with each deploy. Which for an active project can be 8~15 times a day.

A few questions:

  • Is there a flag on a zipFileTree that either make it clean up afterwards, or not expand at all?
  • Is there another way maybe to just iterate the filenames, not using ZipFileTree whatsoever?

I’m not sure if I can aggressively delete the expandedArchives directory as a whole as I also see dependencies from the war task such as from {zipFileTree (configurations.staticWebContent.archiveName} and others being expanded there. Basically whenever we use zipFileTree.


(James Justinic) #2

You can use java.util.zip.ZipFile for this purpose, but the name of each ZipEntry will be the full path, so you would need to handle that. Alternatively, there’s also war.eachFile { } which could be used to check all files going into the war as they’re written instead of after the fact.