Secure Copy using Ant's SCP Task
TechnicalIn 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:
- Download the required jsch library (the one that actually works).
- 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.
- Execute your build script and smile because you didn't have to endure the same frustration.





Loading....