Question

I am using an Ant task from Jar Jar Links to embed classes from a 3rd-party jar file (objenesis) in my distributable jar file (example.jar). Jar Jar will translate classes from the original package (org.objenesis) to one of my choosing.

It works but it leaves empty directories in the distributable jar.

Here is a simplified build.xml:

<target name="jar" depends="compile">
    <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
        classpath="lib/jarjar-1.1.jar"/>

    <jarjar jarfile="dist/example.jar" excludes="org.objenesis.**">
        <fileset dir="build/classes/main"/>
        <zipfileset src="lib/objenesis-1.2.jar"/>
        <rule pattern="org.objenesis.**" result="org.thirdparty.@1"/>
    </jarjar>
</target>   

A sample of contents of the example.jar includes (as expected):

org/thirdparty/Objenesis.class
org/thirdparty/ObjenesisBase.class

but also these empty directories (undesirable):

org/objenesis/
org/objenesis/instantiator/
org/objenesis/instantiator/basic/

My question: how to I exclude these empty directories?

I tried the "zap" option (listed in the doc), but that didn't work.

Was it helpful?

Solution

This appears to be a known issue in Jar Jar, listed in their issue tracker: http://code.google.com/p/jarjar/issues/detail?q=empty&id=32

Given that this was raised almost three years ago and doesn't appear to have got any traction, I suppose your options are to contribute a fix, or to work around it.

An example Ant target to work around it, taking advantage of Ant's support for removing empty directories on copy, would be:

<target name="unpolluted-jarjar" description="JarJars without empty directories">
    <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="${location.lib}/build/jarjar-1.2.jar"/>
    <jarjar basedir="${location.classes}" destfile="${location.dist.binaries}/my-app.jar">
        <zipfileset src="${location.lib}/shipped/dependency.jar"/>
        <rule pattern="com.example.dependency.**" result="com.example.my-app.jarjar.com.example.dependency.@1"/>
    </jarjar>
    <mkdir dir="${location.dist.binaries}/exploded"/>
    <unzip src="${location.dist.binaries}/my-app.jar" dest="${location.dist.binaries}/exploded/my-app.jar"/>
    <copy includeemptydirs="false" todir="${location.dist.binaries}/unpolluted/my-app.jar">
        <fileset dir="${location.dist.binaries}/exploded/my-app.jar"/>
    </copy>
    <jar destfile="${location.dist.binaries}/my-app-unpolluted.jar">
        <fileset dir="${location.dist.binaries}/unpolluted/my-app.jar"/>
    </jar>
</target>

It's a bit grungy, but it achieves what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top