8

我针对 javaee-api 编译我的程序。但是对于 Junit 测试,我必须使用像 glassfish 的 javaee.jar 这样的特定实现来避免java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence之类的错误(另请参见1)。

所以避免使用仅在 glassfish 实现中可用的方法,我想用通用 api 编译我的工件,但用实现 jar 运行 junit。但是两者都提供了相同的命名类和接口,所以类加载器遇到了麻烦。

解决此问题的最佳方法是什么?我可以用maven解决这个问题吗?

非常感谢

4

1 回答 1

14

我认为这是可能的。实际上,从 2.0.9 版本开始,Maven 使用 POM 顺序构建类路径,因此您现在可以对其进行操作。如果你将它与Dependency Scope结合起来,应该可以实现你想要的。实际上,如果您将 GlassFish 的javaee依赖项(带有测试范围)放在依赖项之前javaee-api,则前者应该放在测试类路径中的后者之前,因此单元测试将使用后者,而后者将在编译期间使用。从理论上讲,这应该可行,但它有点脆弱,因此需要仔细记录。

类似的东西(使用虚构的 GFv3 jar):

<dependencies>
  <dependency><!-- this one will be first on the test classpath -->
    <groupId>org.glassfish</groupId>
    <artifactId>javaee</artifactId>
    <version>6.0</version>
    <scope>test</scope>
  <dependency>
  <dependency><!-- this one will be used during compile -->
    <groupId>javax.javaee-api</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
  <dependency>
  ...
</dependencies>
于 2010-01-22T10:26:42.567 回答