Since there was some interest in this topic, I’m diving deeper and showing some code snippets.

As I mentioned before, I’ve found the Archive::Zip and XML::Twig modules invaluable in validating Java deployment descriptors. Post-build validation is important to ensure company standards are adhered to and prevent mishaps, such as deploying an app with a context root already used by another app, or one with no context root defined. It is far easier to fail prior to attempting deployment than to allow deployment to continue, waste valuable time and resources and possibly collide or upset the jvm, costing more time for more people.

I’ve pared down the code I’ve used to the bare essentials for just taking an archive, finding an XML file and extracting it to memory. Next time I’ll get to the XML parsing bit with XML::Twig. Enjoy

# Load modules...

use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use strict;

my $az_ear = Archive::Zip->new();  # Define new zip object

unless ( $az_ear->read( 'foo.ear' ) == AZ_OK ) {  # Read it
die 'read error';                                                  # hopefully

}

# Look for a file
my ($azm_app_xml) = $az_ear->membersMatching('META-INF/application.xml');
print "Found: " . $azm_app_xml->fileName . "\n";

# Extract it to memory

my $app_xml = $az_ear->contents($azm_app_xml);
print "Its contents are:\n$app_xml\n";