2

Project have a dependency to google Guava library, but there is a google-collections (version 1.0) in parent maven project.

How does Maven choose between to Libraries which one to use at compilation?

vach
  • 10,571
  • 12
  • 68
  • 106

3 Answers3

1

Since those two artifacts have different group and artifact IDs, Maven doesn't see them as duplicates and will include both in the build. That means that the class definition that is used when you refer to a class in, for example, com.google.commons.base will be determined by the classpath order.

Maven classpath ordering is deterministic, so place the library you wish to use (presumably, Guava) earlier in your pom and that will be the one used. (To confirm this, View a dependency tree in Maven?)

Mixing artifacts with duplicate classes is generally not a good idea. If you can, standardise on Guava.

Community
  • 1
  • 1
Joe
  • 29,416
  • 12
  • 68
  • 88
0

From the official documentation:

[...] since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.

"nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0

See also the reference book, about Conflict Resolution.

Community
  • 1
  • 1
robermann
  • 1,722
  • 10
  • 19
  • The problem in my case is that I use guava (which includes google-collections) but maven compiles with google-collection dependency defined earlier. – vach Apr 23 '14 at 09:14
  • Declare as an explicit dependency also your wanted google-collections's version – robermann Apr 23 '14 at 09:22
0

I've found solution.

Given: Parent project A Current project B

I've explicitely defined dependency to A and excluded unwanted subdependency. Works like charm.

vach
  • 10,571
  • 12
  • 68
  • 106