Skip to Content

Resolving Classpath Libraries

As mentioned before, Eclipse allows you to add Classpath Libraries to your classpath. A Classpath Library is a set of files and directories that gets added to the classpath. There are two kind of libraries:

  • Static libraries: They simply contribute a fixed set of entries to the classpath. Everywhere you use such a container the same entries are contributed to your classpath. Some of those libraries are contributed by Eclipse plug-ins but you can define your own "User Libraries" via Preferences -> Java -> Build path -> User libraries.
  • Dynamic libraries: Such libraries are resolved each time Eclipse resolves a project classpath. The actual entries the libary contributes to a classpath vary depending on its context. The "Plug-in dependencies" library for example automatically adds all Plug-ins to your classpath, that your (Plug-in)project depent on, according to its Manifest- and plugin.xml-files.

A classpath entry for a Classpath Library has set its kind attribute to con ("Container"). Its path attribute consists of the library type (a "User Library" for example is of type "org.eclipse.jdt.USER_LIBRARY") and it's name (last part of the path). If you use the JUnit library for JUnit4, in your project the classpath entry for this library would look like this:

 <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>

While ant4eclipse has currently built-in support for the "Plug-in dependencies" library, all other kinds of libraries must be declared in ant4eclipse (Note that the JRE library is treated special, see Resolving JREs). You can define a library in two ways:

  • You can use the jdtClasspathLibrary Ant type, that takes a set of resources that will be added to your classpath wherever you reference the library.
  • You can import the User Libraries XML definition file that has been exported from Eclipse

Using the jdtClasspathLibrary

The jdtClasspathLibrary allows you to define a classpath library. The type expects the name of the library (that is the whole path attribute of the classpath entry). To define the resources, that you want to add to your classpath whereever you use the library, use one or more resource collections. Please see Resource Collections in the Ant documentation for more informations on resource collections.

The following example declares the JUnit4 library with all jar-files that are located beneath c:\libs\junit4:

<ant4eclipse:jdtClassPathLibrary name="org.eclipse.jdt.junit.JUNIT_CONTAINER/4">
 <!-- Define that resources that should be added to the classpath.
      Note that you can specify more than one resource collection element
 <fileset dir="c:/libs/junit4" includes="**/*.jar"/>

Importing a User Library

Eclipse allows you to export (and import) your User Library definitions to an XML file. Ant4Eclipse is able to read this file and setup up the User Libraries for your build acordingly. In Eclipse you can export the library definitions via the User Libraries preference page (Preferences -> Java -> Build path -> User libraries -> Export...). The exported file can be read by the userLibraries Ant type. The usage of this type is straightforward, simply pass in the path to your exported file. If you have exported your libraries to "c:\ant4eclipse\all.userlibraries" you would use the userLibraries type this way:

<ant4eclipse:userLibraries userLibraries="c:/ant4eclipse/all.userlibraries"/>


While it's quite handy to use Classpath Libraries in Eclipse, it has some drawbacks when you implement a build, that propably run (automatically) outside of Eclipse:

  • Libraries have to be-redifined in your build file as seen above. That leads to duplicate configuration.
  • Libraries usually point to resources that exists outside of your projects. If you check out your projects from your version control system, this resources might be missing. You have to make sure manually that all needed resources are available (with correct pathed!) whereever you start your build (on different machines, maybe different operating systems).