Executing SSH commands from ANT

Sometimes, when building an application, there is a need for some SSH command execution on the remote server. An example of such command could be:

Have no fear – SSHExec is here!

You can easily achieve remote command invocation, using an ANT Task sshexec. An example of its usage could be this snippet (note that remote.* variables are taken from the *.properties file in this case):

<sshexec host="${remote.host}"
         username="${remote.user}"
         password="${remote.pass}"
         trust="true"
         command="rm -rf ${remote.webapps.path}/ROOT*">

You can read about more detailed examples here.

To add this feature to your ANT installation, you need to download the JSch - Java Secure Channel library and include the jsch jar file into ANT classpath (i.e. in your ANT_HOME/lib/ or – if you’re using an Eclipse – into Eclipse Ant classpath).

I found this feature quite useful as it reduced time I needed to build and deploy an application.