9

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.

Ernst de Haan
  • 467
  • 6
  • 12

1 Answers1

2

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
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Pascal, thank you very much for the answer. I tried both: When I add a file src/main/resources/META-INF/MANIFEST.MF and I run mvn clean test then the file does get copied to target/classes/META-INF/MANIFEST.MF, 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 issue report SUREFIRE-620 for this. – Ernst de Haan May 31 '10 at 20:10
  • @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 May 31 '10 at 20:37
  • 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 Jun 01 '10 at 20:17
  • The first solution Pascal suggested will work in theory. I have files (including Hibernate ".hbm.xml" files) in my "src/main/resources" directory which are accessible to my tests in a Maven surefire run. There must be something unexpected happening behind the scenes. I also looked at your source code and the pom.xml files all seem correctly configured. I did however notice that you are explicitely using version 2.5 of the surefire plugin and version 2.7.1 is now available on Maven Central, have you tryed using the latest version? – Jesse Webb Jan 07 '11 at 20:22
  • Also ensure you are referencing classpath files with the "classpath:/" string otherwise the classes may be trying to find the files relative to their location. – Jesse Webb Jan 07 '11 at 20:23