Skip to content

Instantly share code, notes, and snippets.

@tildedave
Created January 31, 2011 21:41
Show Gist options
  • Save tildedave/804877 to your computer and use it in GitHub Desktop.
Save tildedave/804877 to your computer and use it in GitHub Desktop.
Watch a WAR file for changes, and then deploy it to the root of a running tomcat instance.
#!/usr/bin/perl
use strict;
use warnings;
use File::Monitor;
use File::Path::Expand;
use File::Copy;
use File::Remove;
use App::Rad;
App::Rad->run();
sub setup {
my $c = shift;
$c->register_commands( {
watch => 'Watch a WAR file. arguments: --war=WAR --tomcat=TOMCAT'
});
}
sub redeploy {
my ($war, $tomcat) = @_;
my $webapps_dir = "$tomcat/webapps";
warn "War file $war has changed\n\n";
warn "-- STOPPING TOMCAT --\n";
system("$tomcat/bin/catalina.sh stop");
sleep 5;
warn "-- CLEARING WEBAPPS DIRECTORY --\n";
File::Remove::remove(\1, "$webapps_dir/*");
File::Copy::copy($war, "$webapps_dir/ROOT.war");
warn "-- STARTING TOMCAT --\n";
system("$tomcat/bin/catalina.sh start");
print "Restarted tomcat. Redeployment will likely take ~30 seconds...\n"
}
sub watch {
my $c = shift;
if (!( $c->options->{'war'} && $c->options->{'tomcat'} )) {
return "you didn't give me the correct arguments";
}
my $war = File::Path::Expand::expand_filename($c->options->{'war'});
my $tomcat = File::Path::Expand::expand_filename($c->options->{'tomcat'});
my $webapps_dir = "$tomcat/webapps";
# test war exists
if (not -f $war) {
return "$war file (war) does not exist.";
}
if (not -d $webapps_dir) {
return "$tomcat/webapps directory does not exist.";
}
my $monitor = File::Monitor->new();
my $hasChanged = 0;
my $lastHasChanged = 0;
$monitor->watch( $war, sub {
$hasChanged = 1;
} );
# wait for changes, poll every 3 seconds
$monitor->scan;
print "Waiting for changes...\n";
while(1) {
$lastHasChanged = $hasChanged;
$hasChanged = 0;
$monitor->scan;
if ($hasChanged == 0 && $lastHasChanged == 1) {
&redeploy($war, $tomcat);
}
sleep 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment