Friday, July 12, 2013

Simple war deploy to Tomcat with Gradle

After observing and trying many different Tomcat deployment strategies, I decided to share my current pick.

The Goal: build the war and upload it into Tomcat's "webapps" directory. Should be simple. Thankfuly it is, using int128's Gradle SSH Plugin, a wrapper around JSch.


The main takeaway is that the Gradle SSH Plugin is a nice piece of work, but there are a few other things to note:

  • After the gradle war task has executed the path the new war is war.archivePath.absolutePath (usually in "build/libs").
  • If your deploying to Amazon EC2, (and who isn't?) you'll need to disable StrictHostKeyChecking.
  • Don't upload the war with a .war extension. Tomcat will attempt to start it before it's done copying, fail, and you will have to restart tomcat or screw around in the admin console.
  • Once it's finished uploading, and the old war is removed, then you can rename to .war.
This example doesn't take advantage of Tomcat 7's Parallel Deployment feature, but it could easily be extended to do so by naming the war things like appName##versionNumber.war.

Feel free to post your favorite Tomcat deploy edge cases in the comments!

Example tested with Tomcat 7 and Gradle 1.5.