如何加载一个可选任务到蚂蚁没有-lib或全球安装吗? [英] How to load an optional task into ant without -lib or global installation?

查看:146
本文介绍了如何加载一个可选任务到蚂蚁没有-lib或全球安装吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用FTP任务在蚂蚁,和我找到了适当的jar文件和得到的一切工作正常。我已经把jar文件在库目录一起在构建中使用的其他文件。唯一的问题是,用户必须运行ant -lib公网的FTP-2.0.jar进行构建;我真的preFER,这是可能的,只是运行的蚁族不带任何参数。

I want to use the FTP task in ant, and I have found the appropriate jar files and got everything working fine. I have put the jar files in a "libs" directory alongside the other files used in the build. The only problem is that the user must run "ant -lib commons-net-ftp-2.0.jar" to make a build; I would really prefer that it were possible to just run "ant" with no arguments.

阅读蚂蚁可选任务安装纸页,我看到有五种方式可以加载额外的库在蚂蚁,和他们都不是真的是我要找的。我不想强迫用户做出他们的系统运行此任务的任何修改;它应该有可能只是从库目录加载我们的产品的源文件夹内。因此,这意味着设置CLASSPATH全球也出了(这是一个坏主意,反正)。

Reading the ant optional tasks intallation page, I see that there are five ways one can load up extra libraries in ant, and none of them are really what I'm looking for. I do not want to force the user to make any modifications to their system to run this task; it should be possible to just load it from the "libs" directory inside of our product's source folder. So that means setting the global CLASSPATH is also out (which is a bad idea anyways).

最后一个选项,如文档中所指出的,是preferred办法......从构建脚本自身单独加载jarfiles中。我曾与蚂蚁的contrib任务和JUnit过去做到了这一点,并想在这里这样做,但我不知道怎样才能做到这一点。 FTP任务不支持嵌套的classpath元素,我不知道XML资源,我需要通过的taskdef来加载这个库。如何从蚂蚁内加载库?

The last option, as noted in the documentation, is the preferred approach... loading the jarfiles individually from the build script itself. I have done this in the past with the ant-contrib tasks and JUnit, and would like to do that here, but I don't see how I can accomplish this. The FTP task doesn't support a nested classpath element, and I don't know the XML resource I would need to load this library via a taskdef. How can I load the libraries from within ant?

编辑:为了响应已张贴在这里到目前为止,我使用蚂蚁1.7.1的答案和问题。制作一个ftp的taskdef绝对无法正常工作;抛出了以下错误:

In response to the answers and questions which have been posted here so far, I'm using ant 1.7.1. Making an ftp taskdef definitely does not work; that throws the following error:

构建失败
/my/path/build.xml:13:一流的taskdef org.apache.tools.ant.taskdefs.optional.net.FTP无法找到

BUILD FAILED /my/path/build.xml:13: taskdef class org.apache.tools.ant.taskdefs.optional.net.FTP cannot be found

也许这是因为类名是错误的。究竟如何找到我应该,如果我只有一个jar文件使用的类名?它不记录任何地方,我无法找到罐子本身类似的路径事情。

Perhaps this is because the classname is wrong. How exactly do I find the classname I'm supposed to use if I only have a jarfile? It's not documented anywhere, and I couldn't find anything in the jar itself resembling that path.

推荐答案

您所遇到的问题是由于使用不同的类加载器。下议院网类必须由加载FTP任务相同的类加载器加载。因为FTP任务由蚂蚁装上启动时,您需要将共享网络,以便它是由同一个类加载器加载添加到Ant的类路径中。这就是为什么文档为您提供了4种不同的方式来做到这一点。

The problem you are having is due to the different class-loaders in use. The Commons Net classes must be loaded by the same class-loader that loads the FTP task. Because the FTP task is loaded by Ant on start-up, you need to add the Commons Net to Ant's classpath so that it is loaded by the same class-loader. That's why the documentation gives you 4 different ways to do this.

我同意他们都不是理想(CLASSPATH环境变量是最差)。解决此问题的办法是与您的项目调用Ant和传递apporpriate -lib参数提供一个shell脚本。然后,让人们使用这个,而不是直接调用Ant。事实上,这样它就总是会运行,而不是在道路上现有的蚁族(这仅在当前目录的路径上,领先于其他目录的工作)。

I agree that none of them are ideal (the CLASSPATH environment variable being the worst). One way around this is to supply a shell script with your project that invokes Ant and passes the apporpriate -lib argument. You then get people to use this rather than invoking Ant directly. In fact, you could deviously name it 'ant' so that it gets run instead of the existing 'ant' on the path (this only works if the current directory is on the path, ahead of other directories).

文档中的第五个选项在理论上是很大的。他们终于定格在1.7.0的类加载问题。不幸的是,你提到,没有人改型FTP任务采取的类路径。你可以尝试提交增强请求,但这不会在短期内有所帮助。

The fifth option in the documentation is great in theory. They finally fixed the class-loading problems in 1.7.0. Unfortunately, as you mention, nobody retro-fitted the FTP task to take a classpath. You could try submitting an enhancement request, but this won't help in the short term.

有一个其他选项,这并不比其它更好。相反,在确保共享网络类由加载FTP任务的类加载器加载的,你可以确保FTP任务是由加载共享网络类的类加载器加载。要做到这一点,你必须从Ant安装的'库'目录中删除蚂蚁 - 公 - lib.jar文件。这意味着FTP任务不会获得在启动时加载。这实际上就是为什么可选任务被分解成许多分离的JAR - 使得它们可以被单独移除。把这个JAR文件一起,以便它可以在同一时间被加载的共享网络JAR文件。然后,你可以做这样的事情(我想这和它的作品):

There is one other option, which isn't any better than the others. Instead of making sure that the Commons Net classes are loaded by the class-loader that loads the FTP task, you could make sure that the FTP task is loaded by the class-loader that loads the Commons Net classes. To do this you have to remove the ant-commons-lib.jar file from the 'lib' directory of the Ant installation. This means that the FTP task won't get loaded on start-up. This is actually why the optional tasks are broken up into so many separate JARs - so that they can be individually removed. Put this JAR file alongside the Commons Net JAR file so that it can be loaded at the same time. Then you can do something like this (I tried this and it works):

<taskdef name="ftp"
         classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
  <classpath>
    <pathelement location="${basedir}/lib/ant-commons-net.jar"/>
    <pathelement location="${basedir}/lib/commons-net-2.0.jar"/>
  </classpath>
</taskdef>
<ftp server="yourserver.com"
     userid="anonymous"
     password="blah">
  <fileset dir="somedirectory"/>
</ftp>

不过,这可能是比只使用-lib开关(有或没有包装脚本)更糟糕的选项。我能想到的唯一的另一件事是,试图找到一个第三方FTP任务,而不是使用默认的。

But this is probably a worse option than just using the -lib switch (with or without a wrapper script). The only other thing I can think of is to try to find a third-party FTP task to use instead of the default one.

这篇关于如何加载一个可选任务到蚂蚁没有-lib或全球安装吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆