Ant 可选任务 SSHExec 和 SCP 的问题.类路径问题? [英] Problems with Ant optional tasks SSHExec and SCP. Classpath issue?

查看:29
本文介绍了Ant 可选任务 SSHExec 和 SCP 的问题.类路径问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改 Ant 脚本(目前在 MyEclipse 中使用)以从命令行工作.我这样做是为了让任何人都可以检查项目并在没有 MyEclipse 的情况下构建它.我遇到的问题是 MyEclipse 包含幕后的依赖项.它通过查看工作区的 Ant 配置并根据首选项对话框中选定的库编译类路径来完成此操作.长话短说,我需要获取这些依赖项并使脚本足够智能,以便在没有 MyEclipse 帮助的情况下自行包含它们.

I'm in the process of modifying an Ant script (currently in use from within MyEclipse) to work from the command line. I'm doing this so anyone can check out the project and build it without MyEclipse. The problem I'm running into is that MyEclipse includes the dependencies behind the scenes. It does this by looking at the workspace's Ant configuration and compiling the classpath based on the selected libraries in the preferences dialog. Long story short, I need to take those dependencies and make the script smart enough to include them on its own, without the help of MyEclipse.

让我头疼的任务是 sshexec 和 scp 任务.它们是可选的 ant 任务,需要某个版本的 jsch 才能运行.我从 MyEclipse 的 Ant 类路径中删除了 jsch,并将其添加到项目本身 (lib/dev) 的 lib 文件夹中.MyEclipse 立即抱怨 SSHExec 类找不到依赖类 com.jcraft.jsch.UserInfo,它是 jsch-0.1.44.jar 的一部分.

The tasks that are giving me a headache are the sshexec and scp tasks. They are optional ant tasks that require a version of jsch to run. I removed jsch from MyEclipse's Ant classpath and added it to a lib folder in the project itself (lib/dev). MyEclipse immediately complained that the SSHExec class could not find the dependent class, com.jcraft.jsch.UserInfo which is part of jsch-0.1.44.jar.

我没有看到从构建脚本中为 Ant 设置类路径的方法.我有以下代码,它将 path 元素添加到脚本中,但我认为 Ant 不会使用它,除非明确关联到任务或其他元素.

I don't see a way to set the classpath for Ant from within the build script. I have the following code, which adds a path element to the script, but I don't think Ant uses this unless explicitly associated to a task or another element.

<path id="web-jars">
  <fileset dir="${web-lib}">
    <include name="**/*.jar" />
  </fileset>
  <fileset dir="${app-lib}"> <!-- this is where jsch resides --> 
    <include name="**/*.jar" />
  </fileset>
</path>

看来我需要使用taskdef来定义sshexec和scp任务:

It seems that I need to use taskdef to define the sshexec and scp tasks:

<taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
    classpathref="web-jars"/>

MyEclipse 抱怨这个,"taskdef A class required by class org.apache.tools.ant.taskdefs.optional.ssh.SSHExec cannot be found: com/jcraft/jsch/UserInfo"

MyEclipse complains about this, "taskdef A class needed by class org.apache.tools.ant.taskdefs.optional.ssh.SSHExec cannot be found: com/jcraft/jsch/UserInfo"

它显然在类路径引用、web-jars 中.由于这个格式错误或配置错误的 taskdef,我无法在脚本中运行任何内容.

It's clearly in the classpathref, web-jars. And I can't run anything in the script because of this malformed or misconfigured taskdef.

推荐答案

这里的问题是 SSHExec 类是从类加载器加载的,而该类加载器本身无法访问您的 web-jars 类加载器.为 taskdef 提供这个类路径不会改变这一点.每个类只能从它自己的类加载器和任何父类加载器加载类,但 web-jars 类加载器不是 SSHExec 类加载器的父类加载器(它可能是相反的,因为 SSHExec 似乎在这里找到).

The problem here is that the SSHExec class is loaded from a classloader which itself has no access to your web-jars class loader. Supplying this classpath for the taskdef does not change this. Each class can only load classes from its own classloader and any parent class loaders, but the web-jars classloader is not a parent class loader of SSHExec's class loader (it is likely the other way around, since SSHExec seems to be found here).

看起来像这样:

 ClassLoader    web-jars  ------------->   application CL ------------->  bootstrap CL

 taskdef 
       =>   look for SSHExec here
            => look first in parent class loader
                                     => look for SSHExec here
                                     => look first in parent class loader
                                                                     => look for SSHExec here
                                                                     => not found
                                     => look in our own classpath
                                     => found, load the class
                                     => it somehow uses interface UserInfo
                                     => look for UserInfo here
                                     => look first in parent class loader
                                                                    => look for UserInfo here
                                                                    => not found
                                     => look in our own classpath
                                     => not found, throw exception.

VM 不知道在 web-jars 类加载器中查找 UserInfo(和其他 JSch 类).

The VM has no idea to look for UserInfo (and the other JSch classes) in the web-jars classloader.

我认为 SSHExec 任务位于通常的 ant 类路径中,即由应用程序类加载器加载.然后从 ant 的类路径中删除 SSHExec(或向其中添加 jsch.jar)似乎是这里唯一的解决方案.

I suppose the SSHExec task is somewhere in the usual ant classpath, i.e. loaded by the application class loader. Then removing SSHExec from ant's classpath (or adding jsch.jar to it) seems to be the only solution here.

这篇关于Ant 可选任务 SSHExec 和 SCP 的问题.类路径问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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