Say you’re building a WAR with Maven, and need to do automated deployment via SSH.
Here’s a working Maven antrun plugin def to get you going:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<configuration>
<target>
<echo message="Stopping test server ..." />
<sshexec trust="true" host="test.example.com"
username="root" keyfile="${user.home}/.ssh/id_rsa"
command="service tomcat6 stop" />
<sleep seconds="10" />
<echo message="Copying WAR to test server ..." />
<scp trust="yes"
file="${project.build.directory}/${project.build.finalName}.war"
keyfile="${user.home}/.ssh/id_rsa"
remoteTofile="root@test.example.com:/usr/share/tomcat6/webapps/app.war" />
<echo message="Starting test server ..." />
<sshexec trust="true" host="test.example.com" username="root"
keyfile="${user.home}/.ssh/id_rsa" command="service tomcat6 start" />
<echo message="Waiting for app to get deployed ..." />
<waitfor maxwait="1" maxwaitunit="minute" checkevery="10" checkeveryunit="second">
<http url="http://test.example.com/app" />
</waitfor>
</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.44-1</version>
</dependency>
</dependencies>
</plugin>
This example uses SSH public key authentication (be sure to add the SSH public key of the user running the build, e.g. Jenkins, to the SSH authorized_keys file on the test server).
Tomcat (or whatever servlet container you’re using) is restarted to avoid perm gen errors (here we use an /etc/init.d script).
Now just run mvn clean package antrun:run as part of your CI (e.g. Jenkins) build to deploy.
HTH,
Jukka