Maven 2 - different dependency versions in test and compile

爱⌒轻易说出口 提交于 2019-12-06 18:44:44

问题


I have project that depends on commons-httpclient [2.0] (compile).

I would like to write some jbehave tests - jbehave-core 3.4.5 (test). Both this dependencies depend on commons-lang but in different versions - 1.0.1 and 2.5.

When I execute mvn package I get [BUID FAILURE] in tests section. There is an exception for my testcase in surefire-plugin output:

java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.substringBeforeLast(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;

As I looked in source code - in commons-lang 1.0.1 - indeed, there is no StringUtils.substringBeforeLast(...) method. Why maven uses commons-lang from commons-httpclient (compile) and not from jbehave-core in testing?

I can't afford to exclude this conflicting dependency in commons-httpclient so it must stay in compile time.

So how can this be resolved? - commons-lang 2.5 version in testing and 1.0.1 in compile time.


回答1:


try to define 2 different <dependency> tags with different versions and scopes. Use tag <scope>test</scope> inside dependency for tests and <scope>compile</scope> for compilation.




回答2:


It's a really bad idea to have two different versions for compile and test dependency:

Your non-test code might rely on behavior of the newer JAR and fail when using classes of the older JAR. When you use the older JAR in your tests, the non-test code would fail with the old JAR.

Otherwise you could have used the older JAR anywhere, with the same version... If you get both JAR versions into your classpath, you cannot know which one gets picked up when running your tests. That's also a bad idea.

Hence you should get non-test and test to the same JAR version dependency.



来源:https://stackoverflow.com/questions/6575742/maven-2-different-dependency-versions-in-test-and-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!