如何在Maven/Surefire单元测试运行"MVN测试"期间使清单可用? [英] How do I make the manifest available during a Maven/Surefire unittest run "mvn test"?

查看:182
本文介绍了如何在Maven/Surefire单元测试运行"MVN测试"期间使清单可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Maven/Surefire单元测试运行"mvn测试"期间使清单可用?

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

我有一个开源项目,我正在从Ant转换为Maven,包括其单元测试.这是Maven项目的项目源存储库: http://github.com/znerd/logdoc

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

我的问题与称为基础"的主要模块有关.此模块具有单元测试,该单元测试用于测试类org.znerd.logdoc.Library中的静态方法getVersion()的行为.此方法返回:

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()

getImplementationVersion()方法返回清单文件中的设置值.到目前为止,一切都很好.过去,我已经对此进行了测试,并且只要清单文件确实位于路径META-INF/MANIFEST.MF上的类路径上(在文件系统上或JAR文件中),它就可以很好地工作.

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运行单元测试,但是我的单元测试失败,并显示一条消息,指示Library.getVersion()返回了null.

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

当我要检查JAR时,我发现它甚至还没有生成.在将资源添加到类路径之前,Maven/Surefire针对类运行单元测试.

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.

进一步的调查显示,Surefire在临时目录(例如)中生成了自己的JAR文件.

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

然后使用此JAR加载Library类.该JAR不包含我在src/main/resources下使用的资源.因此,放置META-INF/MANIFEST.MF文件也不起作用.

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.

所以我如何告诉Surefire从与Library类相同的类加载器中获取我的META-INF/MANIFEST.MF文件.

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

请注意,我在Mac OS X 10.6.2和JUnit 4.8.1上使用了Maven 2.2.0,Java 1.6.0_17.

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.

推荐答案

好吧,正如您所指出的那样,问题在于MANIFEST.MF是在package期间生成的,并且直接包含在最终的jar中,而所有这些发生在test.因此,我想您必须要么:

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:

  • 提供您自己的MANIFEST.MF (在package中合并之前在target/classes中可用).我不知道这是否是一个选项(是否可行).
  • 根据JAR,从另一个模块放入并运行测试.
  • 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.

这篇关于如何在Maven/Surefire单元测试运行"MVN测试"期间使清单可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆