سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top