Issue
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.
src="https://i.stack.imgur.com/eUC76.png" alt="dependency">
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.
Solution
Maven 3:
Maven 3 will attempt to obtain the nearest dependency, effectively ensuring that only one of the compile or test scoped dependency is used for both the compile and test phases.
(Thanks Vineet Reynolds)
Maven 2 (OLD):
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.
Answered By - AlexR
Answer Checked By - David Goodson (JavaFixing Volunteer)