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

1

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.

Isocyanide answered 26/5, 2010 at 19:43 Comment(0)
S
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.
Steinke answered 26/5, 2010 at 21:8 Comment(5)
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="jira.codehaus.org/browse/SUREFIRE-620">issue report SUREFIRE-620</a> for this.Isocyanide
@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.Steinke
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.Isocyanide
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?Oeflein
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.Oeflein

© 2022 - 2024 — McMap. All rights reserved.