简体   繁体   English

将用户库添加到Ant Builder类路径

[英]Add user libraries to Ant Builder classpath

I am having a problem with setting up an Ant Builder for my Eclipse projects. 我在为Eclipse项目设置Ant Builder时遇到问题。

I do have several 3rd party libs configured as user libraries within Eclipse. 我确实在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. 我的问题是,如果我想使用Eclipse中的Ant Builder,我将不得不将一些用户库添加到Ant Builder的类路径中以使其正常工作。 I need those Libs because they include several task defs and type defs for Ant, NOT TO COMPILE MY PROJECTS. 我需要那些Libs,因为它们包括几个任务defs和类型defs for Ant,而不是编译我的项目。 But how can I add those user libraries to the Ant Builder classpath? 但是,如何将这些用户库添加到Ant Builder类路径中? 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. 我不想通过手动添加它们来“硬编码”它们,因为如果我将来必须更改一个这些库,我还必须维护所有的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. 而且我不知道如何在Ant中确定运行时用户库的路径以动态加载它们,因为我需要在Ant的引导程序中的那些库能够成功定义我需要的Ant任务。

In that case, you may wish to add your JAR files (containing the taskdefs) to the Ant runtime. 在这种情况下,您可能希望将JAR文件(包含taskdefs)添加到Ant运行时。

Window -> Preferences, select "Ant -> Runtime" from the left. 窗口 - >首选项,从左侧选择“Ant - > Runtime”。 Focus on "Global Entries", then use the "Add JAR" button on the right to add JAR files. 关注“全局条目”,然后使用右侧的“添加JAR”按钮添加JAR文件。

The JAR files you add will be contributed to any Ant process running under Eclipse. 您添加的JAR文件将提供给在Eclipse下运行的任何Ant进程。

Your next question might be - "why do I have to add JARs? Can't I add my user library?". 您的下一个问题可能是 - “为什么我必须添加JAR?我不能添加我的用户库吗?”。 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. 也让我疯狂,我不知道Eclipse为什么不提供这个功能。 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 ). 编辑 2014年2月:事实证明已经请求将用户库添加到Ant的类路径( 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: 我有一个自定义的ant扩展,我包含这种方式:

build.xml: 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. 然后你的libs将被检索到你的临时ant ext lib文件夹中,供ant在运行时使用,但是会从你的项目中排除。 Eclipse has a plugin for Ivy (IvyDE) and this technique has made my life much simpler. Eclipse有一个Ivy(IvyDE)插件,这种技术让我的生活更加简单。

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. 我发现避免必须这样做的最好方法是将ANT_HOME从与Eclipse捆绑的Ant更改为可以在任何IDE,命令行等中使用的独立Ant安装。

Go to Window > Preferences , and in the left pane, select Ant > Runtime . 转到Window > Preferences ,在左窗格中,选择Ant > Runtime Select the Ant Home button on the right of the window and select the root directory of your standalone Ant installation. 选择窗口右侧的Ant Home按钮,然后选择独立Ant安装的根目录。 Eclipse will be kind enough to update the Ant Home Entries in the main pane to use this installation instead. Eclipse将非常友好地更新主窗格中的Ant Home Entries以使用此安装。 I'm using Eclipse Juno here - instructions may be slightly different for other versions. 我在这里使用Eclipse Juno - 其他版本的说明可能略有不同。

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. 现在可以从build.xml文件访问任何放入ANT_HOME / lib的jar,而无需手动将它们添加到类路径中。

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 . 我想赞扬Qasim Rasheed在他的博客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). 它比手动替换Ant Home Entries要好得多(这会导致每次添加新jar时都必须在此屏幕上更新)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM