Add user libraries to Ant Builder classpath

﹥>﹥吖頭↗ 提交于 2019-12-03 13:16:17

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...

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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!