Maven依赖性在测试期间发生冲突(surefire)但不是生产

时间:2013-11-15 15:59:46

标签: maven maven-surefire-plugin maven-shade-plugin

我有一个应用程序需要几个不同版本的相同库。通过intellij运行时,这不是问题,因为它以避免冲突的方式导出类路径。然后我在编译生产时使用maven shade插件创建一个超级jar。

但是,当使用surefire插件运行我的单元测试时,冲突会再次出现。有没有人知道一种方法,我可以确保sure-fire插件以避免这些冲突的方式创建类路径?

谢谢!

更新:以下是我得到的错误(尽管它对我的应用程序非常具体)。基本上有两种版本的Saxon HE和PE都可以加载。运行surefire测试时会导致此异常:

    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.jorsek.appserver.bootstrap.Main.invokeMain(Main.java:130)
    at com.jorsek.appserver.bootstrap.Main.run(Main.java:449)
    at framework.TestUtils.startupDatabase(TestUtils.java:41)
    at framework.AbstractTestInstance.startDatabase(AbstractTestInstance.java:41)
    at framework.AbstractTestInstance.startDatabase(AbstractTestInstance.java:55)
    at org.jorsek.exist.versioning.AllSetslTest.setUpBeforeClass(AllSetslTest.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: java.util.ServiceConfigurationError: javax.xml.transform.TransformerFactory: Provider com.saxonica.config.ProfessionalTransformerFactory could not be instantiated: java.lang.VerifyError: (class: com/saxonica/config/ProfessionalConfiguration, method: newExpressionParser signature: (Ljava/lang/String;ZLnet/sf/saxon/value/DecimalValue;)Lnet/sf/saxon/expr/ExpressionParser;) Wrong return type in function
    at java.util.ServiceLoader.fail(ServiceLoader.java:207)
    at java.util.ServiceLoader.access$100(ServiceLoader.java:164)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:360)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:428)
    at org.exist.validation.XmlLibraryChecker.check(XmlLibraryChecker.java:162)
    at com.jorsek.appserver.jetty.JettyStart.<init>(JettyStart.java:95)
    at com.jorsek.appserver.jetty.JettyStart.main(JettyStart.java:77)
    ... 30 more
Caused by: java.lang.VerifyError: (class: com/saxonica/config/ProfessionalConfiguration, method: newExpressionParser signature: (Ljava/lang/String;ZLnet/sf/saxon/value/DecimalValue;)Lnet/sf/saxon/expr/ExpressionParser;) Wrong return type in function
    at com.saxonica.config.ProfessionalTransformerFactory.<init>(ProfessionalTransformerFactory.java:24)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:310)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:356)
    ... 34 more

1 个答案:

答案 0 :(得分:0)

也许您可以覆盖对测试范围的其他依赖性?或者如果需要,只需在默认范围内覆盖它们。或者你有什么理由不能做上述任何一种情况吗?

或者另一种选择当然是在测试运行期间开始涉及类加载器,如here所示。