Add user libraries to Ant Builder classpath

20,554

Solution 1

In that case, you may wish to add your JAR files (containing the taskdefs) to the Ant runtime.

Window -> Preferences, select "Ant -> Runtime" from the left. Focus on "Global Entries", then use the "Add JAR" button on the right to add JAR files.

The JAR files you add will be contributed to any Ant process running under Eclipse.

Your next question might be - "why do I have to add JARs? Can't I add my user library?". Good question, glad you (were almost about to) asked. Drives me bonkers too and I have no idea why Eclipse doesn't provide this functionality. Maybe it's time to open a feature request...

Edit February 2014: turns out that adding user libraries to Ant's classpath has already been requested (https://bugs.eclipse.org/bugs/show_bug.cgi?id=211669). By the looks of it, I was the one who reopened it...

Solution 2

Im not sure if this will help you, but I like to use Ivy in this situation. I have a custom ant extension which I include that way:

build.xml:

<project name="project" basedir="." default="deploy" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:myNamespace="antlib:org.company.module">
    <property file="build.${user.name}.properties" />
    <property file="build.${env.COMPUTERNAME}.properties" />
    <property file="build.properties" />
    <property file="build-base.properties" />

    <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" />

    <!-- Configuration for external ant libs -->
    <property name="apache.ant.ext.lib" value="${target.root}/antlib" />
    <mkdir dir="${apache.ant.ext.lib}" />
    <ivy:retrieve inline="true" pattern="${apache.ant.ext.lib}/[artifact]-[revision]-[type].[ext]"
        conf="master" organisation="org.company" module="module" revision="1.0" />
    <path id="apache.ant.ext.lib.classpath">
        <fileset dir="${apache.ant.ext.lib}" includes="*.jar" />
    </path>
    <taskdef classpathref="apache.ant.ext.lib.classpath" resource="path/to/antlib.xml" />
...

then your libs will be retrieved into your temporary ant ext lib folder for ant to use during its runtime, but will be excluded from your project. Eclipse has a plugin for Ivy (IvyDE) and this technique has made my life much simpler.

Solution 3

Adding new user/external jars manually is cumbersome - you're right.

I've found that the best way to avoid having to do this is to change your ANT_HOME from the Ant bundled with Eclipse to a standalone Ant installation that can be used from any IDE, command line, etc.

Go to Window > Preferences, and in the left pane, select Ant > Runtime. Select the Ant Home button on the right of the window and select the root directory of your standalone Ant installation. Eclipse will be kind enough to update the Ant Home Entries in the main pane to use this installation instead. I'm using Eclipse Juno here - instructions may be slightly different for other versions.

Now any jars dropped into ANT_HOME/lib will be accessible from your build.xml files without having to manually add them to the classpath.

I'd like to give credit to Qasim Rasheed for showing me this method in his blog at http://www.qasimrasheed.com/post.cfm/eclipse-configure-ant-home. It's much better than manually replacing the Ant Home Entries (which results in your problem of having to update at this screen every time a new jar is added).

Share:
20,554
Nuker
Author by

Nuker

I am software development enthusiastic currently working on several projects on different mobile platforms. Currently targeting iPhone and Windows Phone 7.

Updated on July 09, 2022

Comments

  • Nuker
    Nuker almost 2 years

    I am having a problem with setting up an Ant Builder for my Eclipse projects.

    I do have several 3rd party libs configured as user libraries within Eclipse. Those libraries are added to the build path of my projects and everything is working fine.

    My problem is, that if I want to use the Ant Builder from Eclipse, I will have to add some of the user libraries to the classpath of the Ant Builder to get it working. I need those Libs because they include several task defs and type defs for Ant, NOT TO COMPILE MY PROJECTS. But how can I add those user libraries to the Ant Builder classpath? I don't want to "hard-code" them by adding them manually, because if I must change one those libs in the future, I will also have to maintain all Ant Builders. And I dont't know how load determine the path to the user libraries at runtime within Ant to load them dynamically, because I need those libs at the bootstrap of Ant to be able to define my needed Ant Tasks successfully.