views:

802

answers:

3

Hi,

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!

A: 

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/CoreTasks/unzip.html

http://ant.apache.org/manual/CoreTasks/delete.html

http://ant.apache.org/manual/CoreTasks/jar.html

navr
A: 

You have to unjar and rejar.

<unzip src="myjar.jar" dest="/classes/">
<jar destfile="newjar.jar"
    basedir="/classes/"
    includes="**/*"
    excludes="**/def/*"
/>    
David
+6  A: 

Have you tried using the zipfileset task?

<jar destfile="stripped.jar">
    <zipfileset src="full.jar" excludes="files/to/exclude/**/*.file"/>
</jar>
mipadi
That's pretty slick :)
David
@mipadi Thanks for your answer! I was hoping that there was a method without an intermediate stage - that is directly removing files/ folders from the Jar. Failing which I guess this is the closest I'll get to it!
bguiz
@mipadi Reckon you could take a look at this one, please? http://stackoverflow.com/questions/3837073/ant-jar-and-zipfileset-copy-files-from-one-jar-into-another
bguiz