Getting the right manifest classpath

If you use Apache’s Ant to create archives (jars or wars), you most likely include a manifest(.mf). Well in the past when I’ve needed to note the dependencies on other jars, I’ve would have to come up with some rubbish hack. Things would look something like this:


        <jar destfile="${build.dir}/${core.jar}" basedir="${tmp.build.dir}" index="true">
            <manifest>
                <attribute name="Built-By" value="${authors}"/>
                <attribute name="Implementation-Title" value="core"/>
                <attribute name="Implementation-Version" value="${version} at ${TODAY}"/>
                <attribute name="Implementation-Vendor" value="${company}"/>
                <attribute name="Class-Path" value="foo.jar yep.jar test.jar etc…"/>
            </manifest>
        </jar>

Thanks to Ant 1.7.0, things are a bit different. With the addition of the manifestclasspath task, manifest classpath generation is a bit easier.


       <manifestclasspath property="manifest.classpath" jarfile="${jars.build.dir}/hack.jar">
            <classpath>
                <fileset dir="${jars.build.dir}">
                    <exclude name="junit-4.1.jar"/>
                </fileset>
            </classpath>
        </manifestclasspath>

        <jar destfile="${build.dir}/${core.jar}" basedir="${tmp.build.dir}" index="true">
            <manifest>
                <attribute name="Built-By" value="${authors}"/>
                <attribute name="Implementation-Title" value="core"/>
                <attribute name="Implementation-Version" value="${version} at ${TODAY}"/>
                <attribute name="Implementation-Vendor" value="${company}"/>
                <attribute name="Class-Path" value="${manifest.classpath}"/>
            </manifest>
        </jar>

Note the jarfile attribute is set to ${jars.build.dir}/hack.jar. hack.jar never comes into existence. The reason being is manifestclasspath attempts to create the classpath relative to the items in their actual location. Since this is during a build process, the location is temporal, and the incorrect parent dir is prepended. By convincing the manifestclasspath task that the jars are all in the same dir as the proposed, the jars are added without the unnecessary parent dir. For some seem trivial, but remember, it’s the small things.

This entry was posted in Technical stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.