Problems with Ant optional tasks SSHExec and SCP. Classpath issue?

醉酒当歌 提交于 2019-12-04 20:17:17

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

It looks like this:

 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.

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

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.

Create ~/.ant/lib and copy jsch.jar in there as part of the build initialisation. Any tasks which do scp/sshexec work should depend on this init target.

<target name="init">
  <property name="user.ant.lib" location="${user.home}/.ant/lib"/>
  <mkdir dir="${user.ant.lib}"/>
  <copy todir="${user.ant.lib}">
    <fileset dir="${basedir}/build/tools" includes="jsch-*.jar"/>
  </copy>
</target>

<target name="mytarget" depends="init">
  <scp todir="user@host"><fileset dir="..."/></scp>
</target>

The Ant within Eclipse unfortunately won't pick this up immediately as it does not read ~/.ant/lib on every execution; After running mytarget within Eclipse once and watching it fail, then go to: Window>Preferences>Ant>Runtime and press Restore Defaults - this will add any .jar files from ~/.ant/lib to the Global Entries section and you should be good to go.

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