Secure Copy using Ant's SCP Task

Technical

In all my time using Ant, I've never needed to move or copy files between servers. Instead, I've had to distribute files to customers. In hindsight, that seems a little unusual to me but, until the other day, I'd never used any of Ant's remote copying tasks. When I had to, I was surprised how much time and effort it took to make the process operational so I decided to commit my efforts to "paper".

Aside: If you don't care to read about my trials and tribulations (although it's a spectacularly entertaining read), you can skip to the compiled instructions.

The SCP task in Ant is an optional task and, although the task is available in the Ant core, it's not operational out of the box. To actually use the task, you have to install an external library dependency. The task documentation clearly states this and provides a helpful link to a matrix of library dependencies that are required by various tasks. Find the SCP task in that table and it states that the jsch library, a Java implementation of SSH2, is what's required and, again, provides a helpful link to get it. Easy, right?

Whoa, slow down there, cowboy. It's not.

The library dependency matrix states that the task requires the 0.1.29 version or later of jsch.jar. Follow the link to the jsch homepage and you'll find links to the latest version of the necessary library - 0.1.33. Assuming the traditional numeric sequence hasn't changed since the last time I counted the change in my pocket, 0.1.33 is later than 0.1.29...right? For the sake of argument, let's say yes.

Except it didn't work. I don't know why it didn't work, but I downloaded the jar file, placed it in my classpath - %ANT_HOME%/lib/ - ran my build script and...it hung right in the middle of, guess what? The SCP task execution. Did that about 4 times just to be sure and got the exact same result every time.

Next I spent a little time tracking down previous versions of the library. I'll save you the trouble. They're located here at SourceForge. One by one, I downloaded previous versions. To demonstrate my competence with numeric sequences and the reversing thereof, it went like this: 0.1.32, 0.1.31, 0.1.30, 0.1.29. Once again, allow me to save you the trouble. Don't do that. None of them worked until I downloaded 0.1.29. If I were you, I'd just start there. Seriously.

That was it. Got that installed in my classpath and I was off. Look, I'm not saying this is a tricky install. It's not. The list of tasks themselves is pretty short and simple, but there's a lot of missing, misleading or disinformation out there, so hopefully this will prevent someone else from having to follow the rabbit hole too far.

In Summary

To use the optional SCP task in Ant:

  1. Download the required jsch library (the one that actually works).
  2. Add the library to your classpath. To do this, you can place the jar file in %ANT_HOME%/lib or, if you're running the build through Eclipse, you can place the jar anywhere and specify the jar file location in Window > Preferences > Ant > Runtime.
  3. Execute your build script and smile because you didn't have to endure the same frustration.
tags:
Development, Ant
François Chastanet said:
 
Thank you very much ! I lost one day with the 1.33 version of this library.
 
posted 875 days ago
Add Comment Reply to: this comment OR this thread
 
 
I feel your pain, man. Glad this helped you get there.
 
posted 875 days ago
Add Comment Reply to: this comment OR this thread
 
Ben said:
 
The latest jsch.jar (jsch-0.1.37.jar) is working for me with ant 1.6.5-1.

But thanks for documenting these types of issues.
 
posted 719 days ago
Add Comment Reply to: this comment OR this thread
 
Ben said:
 
I take back my last comment! :-)

For a larger fileset, scp hangs in the middle of transfers using jsch-0.1.37.jar. I linked jsch.jar back to jsch-0.1.29.jar and it works like a charm.

Thanks so much!

See also:
http://notetodogself.blogspot.com/2007/06/ant-scp-...
and
http://www.symphonious.net/2007/10/23/ant-scpssh-t...
 
posted 719 days ago
Add Comment Reply to: this comment OR this thread
 
Rob said:
 
Good to know, Ben. Thanks for speaking up.
 
posted 719 days ago
Add Comment Reply to: this comment OR this thread
 
Avishek said:
 
Thanks a lot Rob !! This was an awesome find... definitely saved me lots of time
 
posted 534 days ago
Add Comment Reply to: this comment OR this thread
 
Jaime said:
 
Since I ended up here while having trouble with Ant's scp and sshexec, I thought It would be a good idea to post and inform that I just used the latest version jsch-0.1.42 and it worked like a million bucks. I did have a large dataset to transfer, so I believe the past troubles where scp hangs are gone. Anyways just an FYI, and thanks a bunch for this post.
 
posted 14 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Rob  Wilkerson