Tuesday, March 22, 2011

Consolidated Java Jars

Sometimes I just want my little Java app to be simple. It's suppose to be a command line client, or some JMS utility, and I just want to move it around easily, call it easily and have it work.

But, it's got a bunch of files, and I used something for a network library, and a command line parsing library...

What I would like is a consolidated jar, one jar that has what is needed, it is configured so it can be run with java -jar myjar.jar, and it will work.

<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}"/>

<jar jarfile="${dist}/Util.jar" basedir="${build}">
<attribute name="Main-Class" value="Main"/>
<zipfileset src="lib/commons-cli-1.2.jar" includes="**/*.class"/>

This ant target will create a jar, and include in the jar, all the class files from commons-cli-1.2.jar. Now, assuming no other dependencies, I should be able to ssh the Util.jar to any destination machine with Java and run it.

No comments: