Search code examples
javaantmanifest.mf

Apache ant manifest class-path?


I have a standard project layout for a java project:

project /
    src /
        source_file_1.java
        ...
        source_file_N.java
    build /
          classes /
              source_file_X.class
              ...
          jar /
              MyJar.jar
    lib /
          SomeLibrary.jar
          SomeOtherLibrary.jar

As far as I can tell, I am building the project correctly with Ant. I need to set the class-path attribute in the Manifest file so my classes can use the required libraries.

The following relevant information from build.xml

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build\classes">
        <classpath id="classpath">
            <fileset dir="lib">
                <include name="**/*.jar" />
            </fileset>
        </classpath>
    </javac>
</target>

<target name="jar" depends="compile">
    <jar destfile="build\jar\MyJar.jar" basedir="build\classes" >
        <manifest>
            <attribute name="Built-By" value="${user.name}" />
        </manifest>
    </jar>
</target>

Any push in the right direction is appreciated. Thanks


Solution

  • Looking at my NetBeans-generated build file, I found this snippet in the -do-jar-with-libraries task:

    <manifest>
        <attribute name="Main-Class" value="${main.class}"/>
        <attribute name="Class-Path" value="${jar.classpath}"/>
    </manifest>
    

    So in other words, it looks like you just need to add another attribute to the manifest task that you already have.

    See also the Manifest Task documentation.