views:

133

answers:

1

How do I make the manifest available during a Maven/Surefire unittest run "mvn test" ?

I have an open-source project that I am converting from Ant to Maven, including its unit tests. Here's the project source repository with the Maven project: http://github.com/znerd/logdoc

My question pertains to the primary module, called "base". This module has a unit test that tests the behaviour of the static method getVersion() in the class org.znerd.logdoc.Library. This method returns:

Library.class.getPackage().getImplementationVersion()

The getImplementationVersion() method returns a value of a setting in the manifest file. So far, so good. I have tested this in the past and it works well, as long as the manifest is indeed available on the classpath at the path META-INF/MANIFEST.MF (either on the file system or inside a JAR file).

Now my challenge is that the manifest file is not available when I run the unit tests:

mvn test

Surefire runs the unit tests, but my unit test fails with a mesage indicating that Library.getVersion() returned null.

When I want to check the JAR, I find that it has not even been generated. Maven/Surefire runs the unit tests against the classes, before the resources are added to the classpath.

Further investigation shows Surefire generates its own JAR file in a temporary directory, e.g.

/private/var/folders/TR/TREvj1wIHYyAcUy-xmc3UU+++TI/-Tmp-/surefirebooter7448562488934426857.jar

And then uses this JAR to load the Library class. This JAR does not contain the resources I stuck under src/main/resources. So putting a META-INF/MANIFEST.MF file also does not work.

So how do I tell Surefire to have my META-INF/MANIFEST.MF file available from the same class loader as the Library class.

Note that I use Maven 2.2.0, Java 1.6.0_17 on Mac OS X 10.6.2, with JUnit 4.8.1.

A: 

Well, as you pointed you, the problem is that the MANIFEST.MF is generated during package and directly included in the final jar and all this occurs after test. So I guess you'll have to either:

  • provide your own MANIFEST.MF (that would be available in target/classes before being merged during package). I don't know if this is an option (and if it will work).
  • put and run your test from another module depending on the JAR.
Pascal Thivent
Pascal, thank you very much for the answer. I tried both:When I add a file <code>src/main/resources/META-INF/MANIFEST.MF</code> and I run <code>mvn clean test</code> then the file does get copied to <code>target/classes/META-INF/MANIFEST.MF</code>, but it is still apparently not accessible to the unit test.When I depend on the JAR from another module within the same project, then the JAR is not used, but the generated classes directory instead. I created <a href="http://jira.codehaus.org/browse/SUREFIRE-620">issue report SUREFIRE-620</a> for this.
Ernst de Haan
@Ernst Well, I didn't help but you're welcome :) I had a big doubt about the first solution but it looks like I was too much confident for the second one. Thank you for posting the Jira issue, it's an interesting case.
Pascal Thivent
@Pascal: I did some more investigation and updated the question. I will do some more searching. Perhaps an "find-additional-class-path-here" kind-of-instruction will work.
Ernst de Haan