我根据 javaee-api 编译我的程序。但对于 Junit 测试,我必须使用 glassfish javaee.jar 等特定实现来避免类似错误java.lang.ClassFormatError:类文件 javax/persistence/Persistence 中非本机或抽象的方法中缺少代码属性(也可以看看1 http://forums.sun.com/thread.jspa?threadID=5320401).
因此,避免使用仅在 glassfish 实现中可用的方法,我想使用通用 api 编译我的工件,但使用实现 jar 运行 junit。但两者都提供相同的命名类和接口,因此类加载器会遇到麻烦。
解决这个问题的最佳方法是什么?我可以用maven解决这个问题吗?
多谢
我认为这是可能的。实际上,从2.0.9版本开始,Maven使用POM命令来构建类路径,因此您现在可以操作它。如果你把它与依赖范围 http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope,应该可以实现你想要的。实际上,如果您将 GlassFishjavaee
依赖性(带有test scope) before the 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)