Deploying a java war to Tomcat

Deploying a war to Tomcat

Deployment of java apps start after compilation, so make sure you're generating your war file in a consistent location. If you need help with that checkout the Java References below.

All we're really doing here is using the :scm => :none, with :deploy_via => :copy, which automatically takes the contents of a directory tars them up, and sends them to a set of servers. Essentially all we needed to do on top of that was setup a symlink from tomcat/webapps to deploy/current on the server, and add in some tomcat stop start commands.

configuration

capfile

 <source lang='ruby'>
 #!/usr/bin/ruby
 load 'deploy'
 set :application, "my-webapp"
 default_run_options[:pty] = true
 # DEPLOYMENT SCHEME
 set :scm, :none
 set :deploy_via, :copy
 set :repository do 
   fetch(:deploy_from)
 end
 # LOCAL
 set :war, "/home/andy/webapps/my-webapp/target/my-webapp.war"
 # TOMCAT SERVERS
 role :webserver, "testserver"
 set :tomcat_home, "/usr/share/tomcat55/"
 set :tomcat_ctrl, "/etc/init.d/tomcat55"
 # USER / SHELL
 set :user, "testuser" # the user to run remote commands as
 set :use_sudo, false
 set :deploy_from do 
   dir = "/tmp/prep_#{release_name}"
   system("mkdir -p #{dir}")
   dir
 end
 # this is capistrano's default location.
 # depending on the permissions of the server
 # you may need to create it and chown it over
 # to :user (e.g. chown -R robotuser:robotuser /u)
 set :deploy_to do 
   "/u/apps/#{application}"
 end
 #
 # simple interactions with the tomcat server
 #
 namespace :tomcat do
   desc "start tomcat"
   task :start do
     sudo "#{tomcat_ctrl} start"
   end
   desc "stop tomcat"
   task :stop do
     sudo "#{tomcat_ctrl} stop"
   end
   desc "stop and start tomcat"
   task :restart do
     tomcat.stop
     tomcat.start
   end
   desc "tail :tomcat_home/logs/*.log and logs/catalina.out"
   task :tail do
     stream "tail -f #{tomcat_home}/logs/*.log #{tomcat_home}/logs/catalina.out"
   end
 end
 #
 # link the current/whatever.war into our webapps/whatever.war
 #
 after 'deploy:setup' do
   cmd = "ln -s #{deploy_to}/current/`basename #{war}` #{tomcat_home}/webapps/`basename #{war}`"
   puts cmd
   sudo cmd
 end
 # collect up our war into the deploy_from folder
 # notice that all we're doing is a copy here,
 # so it is pretty easy to swap this out for
 # a wget command, which makes sense if you're
 # using a continuous integration server like
 # bamboo. (more on this later).
 before 'deploy:update_code' do
   unless(war.nil?)
     puts "get war"
     system("cp #{war} #{deploy_from}")
     puts system("ls -l #{deploy_from}")
   end
 end
 # restart tomcat
 namespace :deploy do
   task :restart do
     tomcat.restart
   end
 end
 #
 # Disable all the default tasks that
 # either don't apply, or I haven't made work.
 #
 namespace :deploy do
   [ :upload, :cold, :start, :stop, :migrate, :migrations ].each do |default_task|
     desc "[internal] disabled"
     task default_task do
       # disabled
     end
   end
   namespace :web do
     [ :disable, :enable ].each do |default_task|
       desc "[internal] disabled"
       task default_task do
         # disabled
       end
     end
   end
   namespace :pending do
     [ :default, :diff ].each do |default_task|
       desc "[internal] disabled"
       task default_task do
         # disabled
       end
     end
   end
 end
 </source>

usage

> cap -T
cap deploy               # Deploys your project.
cap deploy:check         # Test deployment dependencies.
cap deploy:cleanup       # Clean up old releases.
cap deploy:rollback      # Rolls back to a previous version and restarts.
cap deploy:rollback:code # Rolls back to the previously deployed version.
cap deploy:setup         # Prepares one or more servers for deployment.
cap deploy:symlink       # Updates the symlink to the most recently deployed ...
cap deploy:update        # Copies your project and updates the symlink.
cap deploy:update_code   # Copies your project to the remote servers.
cap invoke               # Invoke a single command on the remote servers.
cap shell                # Begin an interactive Capistrano session.
cap tomcat:restart       # stop and start tomcat
cap tomcat:start         # start tomcat
cap tomcat:stop          # stop tomcat
cap tomcat:tail          # tail :tomcat_home/logs/*.log and logs/catalina.out
> cap deploy:setup
  • creates
    • /u/apps/my-tomcat-webapp/
    • /u/apps/my-tomcat-webapp/releases/
  • symlinks :war from :deploy_to/current/xxx.war to :tomcat/webapps/xxx.war
> cap deploy
  • copies :war into :deploy_from
  • tars up :deploy_from
  • copies tar up to servers
  • creates /u/apps/my-tomcat-webapp/releases/{timestamp}
  • expands tar into that folder
  • changes /ui/app/my-tomcat-webapp/current to point to the {timestamp} folder.
  • restarts tomcat