For a java project I'd like to merge all third-party jars it depends on into the main jar created by Apache Ant, which I already managed to do.
The problem is that some of these jar-files have signature-files in their META-INF
-directories, so when I try to run my jar-file, I get the error message "Invalid signature file digest for Manifest main attributes"
. After I delete the signature-files manually the error is gone.
I tried to filter the signature files out in my ant-file with an excludes
-attribute or an exclude
-tag, but nothing seems to have any effect.
This is the ant-task:
<target name="jar" description="Creates the jar file">
<mkdir dir="${jar}"/>
<jar destfile="${jar}/${ant.project.name}.jar" level="9" filesetmanifest="mergewithoutmain">
<zipgroupfileset dir="${lib}" includes="*.jar"/>
<zipfileset dir="${class}"/>
<manifest>
<attribute name="Main-Class" value="${mainclass}"/>
</manifest>
</jar>
</target>
How can I filter files from the resulting jar in this ant-task? Thanks for your help!
Have been struggling with the same issue for a few hours, so ended up writing a new task by extending the existing one:
With the above, all I need in the build file is:
You can use the
exclude
parameter inzipfileset
tag to remove content from merged external JAR files, as this:The resulting JAR file will be unsigned.
carej is right. I've been trying to do this, merging other jars into my application jar excluding some files, and there is no way to use
<zipgroupfileset>
for it.My solution is a variant of the unzip/clean-up/jar method: I first merge all the external library jars into one with
<zipgroupfileset>
, then merge it into mine with<zipfileset>
which does allow filtering. In my case it works noticeably faster and is cleaner than unzipping the files to disk:The first
<jar>
puts all the jars it finds in lib/ into external-libs.jar, then I make it wait for one second to avoid getting warnings about the files having modification dates in the future, then I merge my class files from the build/ directory with the content of external-libs.jar excluding the files in its root, which in this case were README files and examples.Then I have my own README file that lists all information needed about those libraries I include in my application, such as license, website, etc.
To the best of my knowledge there's no way to filter when using <zipgroupfileset>: the include/excludes used there apply to the zips to be merged, not the content within them.
If you have a well-known set of JARs to merge you could use individual <zipset> entries for each one; this approach allows using include/exclude to filter the contents of the source archive.
An alternative approach is to simply unzip everything into a temporary location, remove/modify the unwanted bits, then zip everything back up.
Alberto's answer works fine but takes time to unzip&rezip the archive. I implemented a new Ant task to use built-in filtering functions, that results in much faster execution:
And use it in build file as follows:
I was also facing same problem. Googled a lot and found something that worked for me. Un-jar you jar file delete . META-INF/.SF , META-INF/.DSA files. Jar it again and run it should not show the error message.
Cause of error is explained here: http://qe-cafe.blogspot.in/2010/06/invalid-signature-file-digest-for.html