问题
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.
回答1:
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/classesbefore being merged duringpackage). 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.
 
来源:https://stackoverflow.com/questions/2916209/how-do-i-make-the-manifest-available-during-a-maven-surefire-unittest-run-mvn-t