Jar Jar to the rescue

I think we have all seen a JAR conflict on an application server, I still use JDOM and usually run into a conflict with an older version of the JAR installed somewhere on the system that I usually can’t touch.

So, what I did was to use an Ant task called Jar Jar that is available at: http://code.google.com/p/jarjar/

Jar Jar will take a Jar file or (a group of Jar files), change the package names, and repackage the result in a new Jar File. The idea is that you can create your own Jar file with your dependencies packaged in.

I use the BeanShell scripting language (www.beanshell.org) which is what the LiveCycle ES Workbench Script Object is based on. There is no JAR file that I deploy but a script, therefore no compiled code to deploy. So, I use Jar Jar a little differently then what is documented on Jar Jar’s Wiki. I create my own package for JDOM and one of its dependencies Jaxen and I place the new Jar file in the classpath.

My, Ant file looks like the following:

<project name="CustomJDOM" default="jar" basedir=".">

     <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="./jarjar-1.0rc7.jar"/>

     <target name="jar">

        <jarjar jarfile="jdom_custom.jar">

               <zipfileset src="lib/jaxen-core.jar"/>

              <zipfileset src="lib/jaxen-jdom.jar"/>

             <zipfileset src="lib/jdom.jar"/>

             <rule pattern="org.jaxen.**" result="org.jdom_1_1.jaxen.@1"/>

             <rule pattern="org.jdom.**" result="org.jdom_1_1.@1"/>

        </jarjar>

     </target>

</project>

 

Taking it a line by line:

<project name="CustomJDOM" default="jar" basedir=".">

Standard Ant project tag, the default target is "jar" which kicks of the Jar Jar task.

<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="./jarjar-1.0rc7.jar"/>

This is how you declare a new Ant task (or one of the ways), make sure you update the "classpath" variable to where ever you installed the "Jar Jar" Jar file.

<target name="jar">

I’m declaring a task to be run.

<jarjar jarfile="jdom_custom.jar">

This line kicks-off the Jar Jar task and writes the result to a file called: jdom_custom.jar

<zipfileset src="lib/jaxen-core.jar"/>

<zipfileset src="lib/jaxen-jdom.jar"/>

<zipfileset src="lib/jdom.jar"/>

The list of Jar files that will be repackaged in the new Jar file with a new package name.

<rule pattern="org.jaxen.**" result="org.jdom_1_1.jaxen.@1"/>

<rule pattern="org.jdom.**" result="org.jdom_1_1.@1"/>

I’m changing the package name from org.jaxen to org.jdom_1_1.jaxen and org.jdom to org.jdom_1_1.

To use the new Jar File. Include "jdom_custom.jar" in your classpath and use the new package names. So instead of using the package name: org.jdom in your code use: org.jdom_1_1. You never have to worry about a Jar file conflict again.