且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何加载一个可选任务到蚂蚁没有-lib或全球安装吗?

更新时间:2023-02-22 11:55:16

您所遇到的问题是由于使用不同的类加载器。下议院网类必须由加载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.