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.

5 comments:

Unknown said...

What is the local war file name that is being uploaded? ROOT.war?

Where is the source file name referenced?

Unknown said...

This fails when executed. Can you post something that works?

Unknown said...

Jay: The local file is usually built by gradle to build/libs/[projectname]-[version].war

It did not fail for me, within a valid gradle war project. Can you be more specific about the failure you got?

Unknown said...

Also, what version of gradle are you using, as stated above this was tested with 1.5. You also would need to put host and username with a password or identity file in the remotes area (starting line 18)

Unknown said...

Works great for me!
Thank you.