Re-signing jar files is a pain if there are old signatures to remove. jarsigner happily would add the new signature, rendering the jar invalid if the old signatures are still present. There’s no command line switch to tell jarsigner to remove the old signatures first. So, one has to unpack each jar file first, remove the META-INF/*.SF and *.RSA files, and zip the jar again… NO! The linux version of zip comes with a nice command line option „-d“ to remove files from a zip. So, just execute the following command to remove any existing signatures from a jar file (thanks martinhans!):
zip -d file.jar 'META-INF/*.SF' 'META-INF/*.RSA'
To iterate over a whole bunch of jar files, just embed that command into a bash for loop:
for i in *.jar; do zip -d "$i" 'META-INF/*.SF' 'META-INF/*.RSA';done
In fact, you’re done 🙂
For re-signing mutiple jars: jarsigner is able to read the keystore / key password from a file or from an environment variable, using the -keypass / -storepass command line option together with the :file or the :env modifier.
So, it’s possible to put each of the passwords in a file and use a for loop like this to sign all jars in the current directory using the key key_alias:
for i in ./*.jar; do jarsigner -storepass:file ~/.storepass -keypass:file ~/.keypass "$i" key_alias;done
To make jarsigner read the passwords from an env variable, you’ll have to create those variables first: