David R. Heffelfinger
Groovy Script to SFTP files using AntBuilder and Grapes
Recently I needed to write a Groovy script to SFTP a file to a server. With a little bit of googling, I came to the conclusion that the easiest way to do that was to use AntBuilder ANT's scp task.
Since this script is going to be used by several people, I wanted to use groovy Grapes to pull the dependency automatically.
Turns out that AntBuilder doesn't see dependencies pulled with the Grab annotation, after a little bit of some more googling I found the solution. Instead of using the @Grab annotation, use the static grab() method of the Grape class to pull the dependencies, this allows us to specify that we need the rootLoader as the class loader, which AntBuilder can see.
Without further ado, here is the script in it's entirety for your copying and pasting pleasure:
Grape.grab(group:"ant", module:"ant-jsch", version:"1.6.5", classLoader:this.class.classLoader.rootLoader)
Grape.grab(group:"com.jcraft", module:"jsch", version:"0.1.42", classLoader:this.class.classLoader.rootLoader)
def ant = new AntBuilder();
print "password: ";
//the following line does not work under Cygwin, System.console() returns null
pw = System.console().readPassword();
def pwStr = new String(pw);
Posted at 10:33PM Feb 02, 2010 by David R. Heffelfinger in Groovy |