Thursday, January 15, 2009

Validating Archive Contents With Ant

Have you ever wanted a quick way of validating the contents of an archive as part of your Ant build? I'm sure there are many ways to accomplish this, the most straightforward being to inflate it to a temporary directory, but yesterday I crafted up this little nugget to validate an archive in-place and it seems to work great. And the best part is there's no messy cleanup needed afterwards!

<!-- Compute the difference of the actual JAR and the expected zip entries -->
<resources id="artifact.set.discrepancies">
<!-- Create a resource set from the actual archive -->
<zipfileset src="${}/some_archive.jar" includes="**/*" />
<!-- Create a resource set of expected items -->
<zipentry archive="${}/some_archive.jar" name="afile.ext"/>
<zipentry archive="${}/some_archive.jar" name="anotherfile.ext"/>
<zipentry archive="${}/some_archive.jar" name="a/path/to/afile.ext"/>
<zipentry archive="${}/some_archive.jar" name="some/other/path/to/afile.ext"/>
<zipentry archive="${}/some_archive.jar" name="META-INF/MANIFEST.MF"/>
<pathconvert property="set.difference" refid="artifact.set.discrepancies" />
<condition property="artifacts.verified">
<resourcecount count="0" when="eq" refid="artifact.set.discrepancies" />
<!-- Fail if any differences are found -->
<fail unless="artifacts.verified"
message="The following artifacts were not expected: ${set.difference}" />

Note: I believe a minimum version of Ant 1.7 is required for the resources stuff to work.

No comments: