22

I am trying to rearchitect my build technique for creating Java jar files which depend on common 3rd party jar files. (GlazedLists, Apache Commons, etc.)

I had been chucking them all into {Java JRE dir}/lib/ext so they would automatically be seen by the JRE, but that led to problems like not remembering that I need to distribute certain jar files, so I'd like to learn to be more explicit.

So I moved them all into c:\appl\java\common\, added them to the Eclipse build path, aand defined this in my ant file:

<path id="javac_classpath">
    <fileset dir="${libDir}">
        <include name="*.jar"/>
    </fileset>
    <fileset dir="c:/appl/java/common">
        <include name="*.jar"/>
    </fileset>
</path>

I have my Class-Path manifest header set to "." in my jar task but that doesn't seem to work even if I put the relevant jar files into the same directory as my application jar file. I can add them all manually one-by-one to the Class-Path header, but I'm wondering, is there an easier way to get the Class-Path header setup properly?

6 Answers 6

38

This is all you need:

<path id="build-classpath">
    <fileset dir="${dist}/lib">
        <include name="*.jar"/>
    </fileset>
</path>
<manifestclasspath property="lib.list" jarfile="${dist}/lib/myprog.jar">
    <classpath refid="build-classpath"/>
</manifestclasspath>
<jar jarfile="${dist}/lib/myprog.jar"
     basedir="${build}"
     includes="com/my/prog/**" >
    <manifest>
        <attribute name="Main-Class" value="com.my.prog.MyProg"/>
        <attribute name="Class-Path" value="${lib.list}"/>
    </manifest>
</jar>

As you can probably see, it assumes you've compiled your Java classes and output them to ${build}. It also assumes you've copied your jarfiles to ${dist}/lib.

That said, it would be worth looking into other build systems which have built-in support for dependencies, such as Maven and Gradle. These other build systems have already thought through many common project structures and build operations, so you don't have to script everything down to the last detail.

3
  • Hmmm. Correction: it doesn't work for a fresh build, because the ${dist}/lib dir may not exist yet. Is there a way to delay the execution of the <path> task?
    – Jason S
    Aug 3, 2009 at 16:51
  • DOH! never mind, I had put the <path> and <manifestclasspath> in the root section of my ant file, just fixed it by putting it into the jar task. thanks again!!!!!
    – Jason S
    Aug 3, 2009 at 16:54
  • 1
    I just found your answer, and wanted to thank you. It really has helped me out a lot!
    – v010dya
    Oct 9, 2015 at 19:24
4

What you want is a mapper. Flattenmapper is probably the easiest, but essentially you need to create a path element which specifies your classpath, then use a pathconvert on it to turn it into a string that can be included in your manifest.

Edit: you can use include rules to build the path element, so everything in a directory ending with jar would be **/*.jar

3

With ant 1.7 and after, the manifestclasspath task is the way forward. If you are using a version of ant prior to 1.7, the manifestclasspath task does not exist.

The following gives the same result: -

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

<pathconvert property="lib.list" pathsep=" ">
    <path refid="build.classpath" />
    <mapper>
        <chainedmapper>
            <flattenmapper />
        </chainedmapper>
    </mapper>
</pathconvert>

<target name="generate.jar">
    <jar destfile="${dist}/lib/myprog.jar">
        <manifest>
            <attribute name="Main-Class"
                value="com.mypackage.MyClass" />
            <attribute name="Class-Path" value="${manifest.classpath}" />
        </manifest>
    </jar>
</target>
1
  • In generate.jar, shouldn't the reference to manifest.classpath be lib.list?
    – TonyG
    Jan 22, 2019 at 0:12
0

Classpath directories do not, by default, include Jar files for security reasons. Someone could place a jar file in there, and it could be loaded up, overriding your code, without your knowledge. I do know that there was discussion about adding another flag or ending token to allow Jar files to be added to the classpath, but, if my memory serves, that is slated for version 7. However, I could be wrong on the last part.

0

The Class-Path manifest header for jar files requires you to explicitly list the jar files you want to include. Listing a directory in the classpath means java will look for .class files in that directory, not jarfiles.

-1

Since you're in Ant, you probably don't want to switch, but Maven is pretty good at identifying all your dependencies, and has several facilities for copying or combining your lib jars together for distribution purposes. For my purposes I have a Maven plugin that creates a run script during build, which in turn sets the classpath for me so I don't need to worry about it.

1
  • Ivy is a pretty ant-compatible way to do dependency management. Oct 19, 2009 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.