3

I have a war file foo.war in my current working directory. I have a MANIFEST.MF file, too.

ls .
foo.war MANIFEST.MF

Within foo.war, there is a file foo/META-INF/MANIFEST.MF. I want to replace that file inside the war with the file in my working directory. I think zip can be used to do this, but I don't understand the zip documentation.

What would be the correct usage?

How can I verify that it was done correctly?

How would I do that with jar?

Gustave
  • 2,787
  • 4
  • 27
  • 56
  • I saw you commented on [this question](http://stackoverflow.com/questions/4799553/how-to-update-one-file-in-a-zip-archive). Have you tried the answer suggesting the `jar` command ? – Aserre Jul 22 '15 at 12:21
  • I looked at the jar documentation. After that I assume that jar does more stuff that what I want (it is able to create a manifest file) and I don't know how to disable that functionality. And it is unclear to me if jar is specific to real jar files only or if it is usable for war files, too (there are different specs for war, ear and jar files). – Gustave Jul 22 '15 at 13:17

2 Answers2

2

Try placing the MANIFEST.MF in a directory called foo/META-INF/

then run:

zip -f foo.war foo/META-INF/MANIFEST.MF

to verify the file run:

unzip -l foo.war

use the same for .jar files

nando
  • 351
  • 3
  • 6
  • That doesn't work: I get zip warning: name not matched: foo/META-INF/MANIFEST.MF – Gustave Jul 22 '15 at 12:04
  • unzip -l foo.war just lists all the files in the war, I cannot see that way if the MANIFEST-MF file was replaced. – Gustave Jul 22 '15 at 12:09
  • If I use zip -f foo.war META-INF/MANIFEST.MF, the command doesn't emit a warning, but it does not replace the file. – Gustave Jul 22 '15 at 12:17
  • To verify I used unzip -p foo.war META-INF/MANIFEST.MF >MANIFEST.MF. – Gustave Jul 22 '15 at 12:17
  • the `-f` option for zip replaces the file only if it has been modified or has a newer timestamp, you have to run it from the same directory that you generated the war file. I assumed that you wanted to replace it for a newer file. That's why I suggested the `unzip -l` to verify it, the idea was to check the timestamp. Sorry if it didn't worked. – nando Jul 22 '15 at 15:33
2

After experimenting a while I found this solution:

#! /bin/bash

set -e

apt-get install -y zip
apt-get install -y unzip

rm -f foo.war
cp foo.original.war foo.war
touch META-INF/MANIFEST.MF

zip -f foo.war META-INF/MANIFEST.MF

rm -f test

unzip -p foo.war META-INF/MANIFEST.MF >test

#Uncomment next line to test verification:
#echo "X" >>test

if diff test META-INF/MANIFEST.MF; then
  echo "OK."
else
  ERR=$?
  echo "Failed (diff exit code $ERR)."
  exit 1
fi
  • zip seems to replace the file only if the time stamp is new enough. What "new enough" exactly means, is still unclear to me.
  • I extracted the MANIFEST.MF file from zip using unzip -p foo.war META-INF/MANIFEST.MF >test
  • I use the exit code of diff to verify MANIFEST.MF has really been replaced.
  • zip spits out the warning: "zip warning: Local Entry CRC does not match CD: META-INF/MANIFEST.MF" The meaning of this warning is unclear to me.
  • Do I really have to put MANIFEST.MF inside the META-INF folder or is there a possibility to specify the source location of the file that should be replaced?
  • I don't like that "touch". Is there a way to force the replacement without fiddling with timestamps?
  • What about character encoding, unusual file names and line endings?
Gustave
  • 2,787
  • 4
  • 27
  • 56