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">
<difference>
<!-- Create a resource set from the actual archive -->
<zipfileset src="${path.to}/some_archive.jar" includes="**/*" />
<!-- Create a resource set of expected items -->
<resources>
<zipentry archive="${path.to}/some_archive.jar" name="afile.ext"/>
<zipentry archive="${path.to}/some_archive.jar" name="anotherfile.ext"/>
<zipentry archive="${path.to}/some_archive.jar" name="a/path/to/afile.ext"/>
<zipentry archive="${path.to}/some_archive.jar" name="some/other/path/to/afile.ext"/>
<zipentry archive="${path.to}/some_archive.jar" name="META-INF/MANIFEST.MF"/>
</resources>
</difference>
</resources>
<pathconvert property="set.difference" refid="artifact.set.discrepancies" />
<condition property="artifacts.verified">
<resourcecount count="0" when="eq" refid="artifact.set.discrepancies" />
</condition>
<!-- 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: