问题描述
在将Java EE 6 API作为提供范围的依赖项的maven项目中的测试中尝试使用SOAP时,我遇到了麻烦.
I'm finding troubles when trying to use SOAP in tests in a maven project that has Java EE 6 API as a provided-scoped dependency.
重要事实
- 我知道该API无法用于测试
- 我知道如果要使用任何规范,我应该导入该规范的实现
- SOAP是Java SE的一部分,而不是EE
- 中包含SOAP(模拟)类,其中 Maven工件,我相信他们不应该
- 这发生在Java EE 6中,但是那些类具有在版本7中被删除
- 如果删除依赖项,则测试可以正常工作
- 使用7.0版的API时,一切正常
- I'm aware that the API is unusable for testing
- I know I should import an implementation of any specification if I want to use it
- SOAP is part of Java SE, not EE
- SOAP (mock) classes are included in the Maven artifact and I believe they should not
- This happens in Java EE 6, but those classes have been removed in version 7
- If I remove the dependency, the tests work fine
- When using 7.0 version of the API everything works
例外
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/xml/messaging/URLEndpoint
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:102)
at somecompany.someproject.service.SomeBean.someMethod(SomeBean.java:##)
at somecompany.someproject.service.SomeBeanTest.testSomeMethod(SomeBeanTest.java:##)
POM配置
<dependencies>
...
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
...
</plugin>
...
</plugins>
...
</build>
失败的解决方案
我尝试了几种解决方案(为了清楚起见,不会发布每个解决方案,但会根据请求发布它们):
I have tried several solutions (not going to post each one for clarity, but will post them by request):
- 我已经在Maven中尝试了每个
<scope/>
,但没有一个在测试中禁用该库 - 此外,尝试将库作为插件的依赖项排除在外
- 将rj.jar作为具有比javaee-api更高优先级的系统依赖项也不起作用
- I have tried every single
<scope/>
in Maven and none disables the library in tests - Also, tried excluding the library as a dependency of the plugin
- Including rj.jar as a system dependency with a higher priority than javaee-api one didn't work either
不需要的解决方法
我使用7.0依赖项进行了测试,但是根据政策,这是不可接受的,因此我需要对其进行修复.
I got the tests working using 7.0 dependency but this is unacceptable according to policies, so I need to fix this.
推荐答案
我必须使用maven-surefire-plugin
的属性classpathDependencyExcludes
使其起作用:
I got to make it work using the property classpathDependencyExcludes
of the maven-surefire-plugin
:
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
...
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExcludes>javax:javaee-api</classpathDependencyExcludes>
</classpathDependencyExcludes>
...
</configuration>
...
</plugin>
...
</plugins>
...
</build>
我将保留此帖子,因为我相信在某个时候我不会是唯一一个遇到这种情况的人.
I'm going to leave this posted as I believe I'll not be the only one experiencing this at some point.
这篇关于Maven测试具有Java EE 6依赖关系的SOAP的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!