How to write an ant task that removes files from a previously compiled JAR?
Let's say the files in my JAR are:
aaa/bbb/ccc/Class1
aaa/bbb/ccc/Class2
aaa/bbb/def/Class3
aaa/bbb/def/Class4
... and I want a version of this JAR file without the aaa.bbb.def
package, and I need to strip it out using ant, such that I end up with a JAR that contains:
aaa/bbb/ccc/Class1
aaa/bbb/ccc/Class2
Thanks!
Have you tried using the zipfileset
task?
<jar destfile="stripped.jar">
<zipfileset src="full.jar" excludes="files/to/exclude/**/*.file"/>
</jar>
For example:
<property name="library.dir" value="dist"/>
<property name="library.file" value="YourJavaArchive.jar"/>
<property name="library.path" value="${library.dir}/${library.file}" />
<property name="library.path.new" value="${library.dir}/new-${library.file}"/>
<target name="purge-superfluous">
<echo>Removing superfluous files from Java archive.</echo>
<jar destfile="${library.path.new}">
<zipfileset src="${library.path}" excludes="**/ComicSans.ttf"/>
</jar>
<delete file="${library.path}" />
<move file="${library.path.new}" tofile="${library.path}" />
</target>
You have to unjar and rejar.
<unzip src="myjar.jar" dest="/classes/">
<jar destfile="newjar.jar"
basedir="/classes/"
includes="**/*"
excludes="**/def/*"
/>
If a jar-file capable archiver program, like e.g. "zip" on Linux, is available, the task can be done by
<exec executable="zip">
<arg value="-d"/>
<arg value="myJarCopyToStrip.jar"/>
<arg value="aaa/bbb/def/*>
<arg value="aaa/bbb/def>
</exec>
Subtree deletion depends on the capabilities of the used archiver.
The "os" attribute of the Ant "exec" task allows to use different archivers on different OS's.
I came here looking to use ant as a workaround some short comings in gradle unzip.
On the off chance someone else is in the same boat.
Here is an example:
task antUnzip() << {
ant.jar(destfile : "stripped.jar") {
zipfileset (src : "full.jar", excludes : "files/to/exclude/**/*.file") {
}
}
}
I am not sure if there a direct solution for your requirement. I would recommend to explode the jar to some temp directory and then remove unwanted class files. Finally create a new jar with required class files.
Reference links:
http://ant.apache.org/manual/Tasks/unzip.html
http://ant.apache.org/manual/Tasks/delete.html
http://ant.apache.org/manual/Tasks/jar.html