Question

Given an Ant path element like the following one:

<path id="compile.classpath">
    <pathelement location="${common.jar}"/>
    <pathelement location="${servlet.jar}"/>
    <pathelement location="${j2ee.jar}"/>
    ...
</path>

How can I create an uber-jar that includes all the files in the path without having to spell out all the file names again? The following works, but contains duplicate code, and is not what I want:

<jar jarfile="uber.jar" ...>
    <manifest> ... </manifest>
    <zipfileset src="${common.jar}"/>     // UNWANTED code duplication
    <zipfileset src="${servlet.jar}"/>
    <zipfileset src="${j2ee.jar}"/>
</jar>
Was it helpful?

Solution

You should be able to do this:

<jar destfile="über.jar">
    <manifest>
        <attribute name="Main-Class" value="${entry.point.class}"/>
    </manifest>
    <path refid="compile.classpath"/>
</jar>

I don't understand why the common.jar would have unwanted duplicate code. Also, you normally don't include jars inside a jar.

I think what you really want to create is a War File. A war file can contain multiple jars, plus the classes you want to execute (the ones you compiled via the <javac> task). In the META-INF/MANIFEST.MF file, you include a Main-Class: entry pointing to the class that should be the entry point of your war. Users will be able to execute your war as:

 $ java -jar my.war

OTHER TIPS

Alternatively, you could create an executable jar with the classpath listed inside the manifest file:

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