A few weeks ago, I asked a question on StackOverflow: how can you test a Maven build?
EqualsVerifier a complex Maven build, and I want to have some automated checks that the jar files and pom files produced by the build actually contain what I expect them to contain.
For instance, I want to check for the presence of an Automatic-Module-Name entry in the manifest file. It’s a multi-release jar, so I also want to check that the proper class files exist inside of the jar file’s
META-INF/versions directory. Finally, EqualsVerifier is published to Maven Central, so I also want to check that the produced pom file contains the dependencies the project needs, but that the produced pom file for the fat jar that I also publish, doesn’t contain these dependencies.
Unfortunately, it’s hard to google for this, because of the words I would use to describe this (“test”, “verify”) already have specific different meanings in Maven.
I got a nice response from Karl Heinz Marbaise, one of the Maven devs, who suggested I create an additional Maven submodule, use a plugin to copy the relevant artifacts to a directory, and go from there.
So I created the
equalsverifier-release-verify submodule in the project and used
copy-rename-maven-plugin to copy the files, as follows:
Now the poms and jars are in the
src/test/resources folder, I can work with them. For the pom files, I used Java’s built-in XPath API, because it’s simple, and my needs are simple as well. But you can use whatever you want.
For the jar files, I used NIO to access their content as a FileSystem:
Now I can get a list of files that exist in the jar:
Or read the content of a file to check the content of the manifest:
I can even check if files are compiled to the correct Java version:
(Here is a description of Java’s class file format, including a list of Java major class file versions.)
Note that for this post, I didn’t bother handling exceptions or closing resources. Filling in those blanks is left as an exercise for you, dear reader 😉.
This was a pretty fun thing to play around with! If you want to see the full code, take a look at the
equalsverifier-release-verify submodule on GitHub!