combine multiple jar files and remove signatures
I just came across a situation where I had to join multiple jar files and my own classes into one jar bundle. Ant comes in handy:
<jar destfile="${app.id}-${app.version}.jar"
index="true"
filesetmanifest="merge">
<fileset dir="${classes.dir}"/>
<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
</jar>
This takes all the files in ${classes.dir}, packs them in a jar and add the content of all the jar files in ${lib.dir}. In this case, we merge multiple manifests, but this can be changed according to the jar task documentation.
This works as long as none of your files is signed. You know that you are in trouble when you get messages like
java.lang.SecurityException: no manifiest section for signature file entry ....
I found a solution here, and the say that one has to remove the SUN_MICR files. This can be done manually, but keep it in mind when ever you update your libraries. I find it more elegant when ant just removes the files. Unfortunately it is to possible to specify excludes for zipgroupfileset. The alternative to zipgroupfileset is zipfileset, which allows exclude patterns and extraction of zip files via the src parameter. And thats the problem. You can only specify exactly one file as src. I don’t want to specify all my jars manually. Therefore I creates a single jar first, using zipgroupfileset and then move this file through a zip/zipfileset where we can exclude specific files.
<jar destfile="${app.id}-${app.version}.jar"
index="true"
filesetmanifest="merge">
<fileset dir="${classes.dir}"/>
<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
</jar>
<zip destfile="temp-jar-file.jar">
<zipfileset src="${app.id}-${app.version}.jar" excludes="META-INF/*.RSA, META-INF/*.DSA, META-INF/*.SF" />
</zip>
<move file="temp-jar-file.jar" tofile="${app.id}-${app.version}.jar"/>
You end up with a single, signature free jar file. This is not the most elegant solution, which would be either to enable multiple src files in zipfileset or an excludes parameter for zipgroupfileset that matches against the content of files, not the input files. But still, its small and does not need any third party ant tasks.
This is awesome !!! Good work
it’s fantastic!!!!!!!!!
You helped me so much