Generate manifest class-path from in Ant


In the build file below, the jar target refers to the jar.class.path property for the manifest class-path. The compile target refers to project.class.path

There is redundancy here, because jar.class.path and project.class.path are very similar. They must be both updated when libraries are added, which can be a pain if the list of libraries gets very long. Is there a better way? Any solution must be cross-platform and always use relative paths.

It should generate the JAR classpath from a fileset and not the other way around, so I can use wildcards to e.g. include all JAR files in a directory.

<?xml version="1.0"?>
<project name="Higgins" default="jar" basedir=".">

    <property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>

    <path id="project.class.path">
      <pathelement location="build"/>
      <fileset dir="lib">
        <include name="forms-1.2.0.jar"/>
        <include name="BrowserLauncher.jar"/>

    <target name="prepare">
        <mkdir dir="build"/>

    <target name="compile" depends="prepare" description="Compile core sources">
        <javac srcdir="src"
          <classpath refid="project.class.path"/>

    <target name="jar" depends="compile" description="Generates executable jar file">
        <jar jarfile="higgins.jar">
                <attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>
                <attribute name="Class-Path" value="${jar.class.path}"/>
            <fileset dir="build" includes="**/*.class"/>            
            <fileset dir="src" includes="**/*.properties"/>         


Best Solution

<path id="build.classpath">
  <fileset dir="${basedir}">
     <include name="lib/*.jar"/>

<pathconvert property="manifest.classpath" pathsep=" ">
  <path refid="build.classpath"/>
       <globmapper from="*.jar" to="lib/*.jar"/>

<target depends="compile" name="buildjar">
  <jar jarfile="${basedir}/${test.jar}">
     <fileset dir="${build}" />
       <attribute name="Main-Class" value="com.mycompany.TestMain"/>
       <attribute name="Class-Path" value="${manifest.classpath}"/>

For further information check out this article.

Related Question