Java – way to specify the location of a local jsch.jar from within build.xml

antjavascp

build.xml contains <scp> and <sshexec> tasks, so I provide jsch.jar and
other libraries in the same directory together with build.xml.

The following taskdef:

<taskdef name="scp"
    classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp"
    classpath="WebContent/WEB-INF/lib/jsch-0.1.43.jar" />

throws an error

A class needed by class org.apache.tools.ant.taskdefs.optional.ssh.Scp
cannot be found: com/jcraft/jsch/UserInfo

I cannot modify the standard Ant installation (e.g. put jsch.jar in ant lib
directory, or remove ant-jsch.jar), or add command-line flags, or modify
system environment variables, etc.: the script has to run with default Ant
on different systems.

I'm actually reposting the question originally asked here:
http://ant.1045680.n5.nabble.com/specifying-location-of-an-external-library-within-build-xml-td1344969.html

but could not get the answer about classloader to work.

Best Answer

Finally I found a working solution (for Ant 1.7.1 at least). First you have to remove ant-jsch.jar from ANT_HOME/lib as Ant complains about it and gets confused. Then load libraries from the project itself:

<available property="ant-jsch.present" file="${ant.home}/lib/ant-jsch.jar"/>
<fail if="ant-jsch.present" message="Please remove ant-jsch.jar from ANT_HOME/lib see [http://ant.apache.org/faq.html#delegating-classloader]"/>

<path id="jsch.path">
    <pathelement location="lib/ant-jsch.jar" />
    <pathelement location="lib/jsch-0.1.44.jar" />
</path>

<taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp" classpathref="jsch.path" />
<taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec" classpathref="jsch.path" />
Related Topic