Can I add another file to an existing jar/war file?

How can I add an additional file to an existing jar/war file using gradle? The equivalent shell/jar command is:

jar uf myjar.jar newfile.txt


I don’t know what’s the exact use case. I suggest to create a new jar that contains the contents of a previous jar plus whatever other stuff you need.

Also, take a look at dsl reference on war/jar and user guide content on what stuff gets into the war/jar by default. Depending on your exact use case you might find a more appropriate solution.

Hope that helps!

You can create a new jar with the extra file like this:

task combinedJar(type: Jar) {
  from zipTree("path/to/my.jar")
  from "path/to/file/to/include.txt"

You could call the ant.jar task to update the JAR. I do something like this to update a JAR with an INDEX.LIST file (for an applet).

task updateJarWithIndex(dependsOn: copyWarRuntimeJars) << {
        ant.jar(update: "true", index: "true", destfile: "${project(':gui').libsDir}/uwm-gui.jar") {
            indexjars {
                fileset(dir: "$buildDir/output/lib")