<< All your READ_UNCOMMITTED and REPEATABLE_READ transactions are belong to us | Home | A past life, back up in >>

FTP using Groovy and Ant

Lately, I've been writing a lot of scripts in Groovy. Frankly, I love it. It's easy to augment the functionality of your Java classes -- implement interfaces, extend classes etc. Another nice feature is that Groovy allows you to leverage the power of Ant in your scripts using the AntBuilder object. I find this incredibly useful when doing off-liney, scripty things I'd normally do using bash combined with other Unix tools.

One common scripting task is to transfer a bunch of files to an FTP server. Here's how I would accomplish FTP'ing *.gz files in the current directory using bash and curl:

for f in *.gz ; do
    curl -s -S -P - -T $f ftp.foo.com
done
Hard to beat for compactness. But, if you're already working in Groovy, you can use Ant's FTP task to accomplish the same thing. Here's the Groovy code:
ant = new AntBuilder()
ant.ftp( server:"ftp.foo.com",
    userid:"user",
    password:"passwd",
    passive:"yes",
    verbose:"yes",
    remotedir:"/pub/incoming",
    binary:"yes" ) {
        fileset( dir:"." ) {
            include( name:"**/*.gz" )
        }
    }
The verbose setting is helpful because otherwise the command's operation is silent. With verbose enabled, output will be sent to stdout. All options supported by Ant's FTP task can be used in the Groovy context. Furthermore, nested XML elements in Ant become closures in Groovy. (The fileset and include statements above.)

FTP is an optional Ant task so if you want to use it in your scripts, you'll need the following dependencies on your classpath:

  • ant.jar
  • ant-nodeps.jar
  • ant-apache-oro.jar
  • ant-commons-net.jar
  • jakarta-oro.jar (at least version 2.0.8)
  • commons-net.jar (at least version 1.4)
I've tested this with Ant 1.6.5 only.




Add a comment Send a TrackBack