27

I have defined version 4.3 in the parent pom for a library A , but in the project module specified by the child pom , version 2.5 of A is required. The issue I am facing is that both the versions are being kept and hence I am getting conflicts.

Please advise how to resolve the issue.

Rndm
  • 6,710
  • 7
  • 39
  • 58
  • Is version 2.5 required by your own code or by some plugin or dependency? – Andrew Logvinov Oct 25 '12 at 17:50
  • By a dependancy and the code too. – Rndm Oct 25 '12 at 17:51
  • 1
    I am having the same issue. Still haven't managed how to override parents dependency version. Been hinted to and bom. But haven't worked for me yet. This post is similar to my issue https://stackoverflow.com/questions/56382459/maven-parent-pom-vs-bom-dependency-management – Laguh Oct 09 '19 at 16:23

2 Answers2

10

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but 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

It sounds like A version 2.5 is being included transitively by another dependency. This puts both version 4.3 and 2.5 at the same length.

  • Project -> Parent -> A 4.3
  • Project -> Dependency -> A 2.5

By explicitly defining dependency of A 2.5 in your project it will then be the nearest and override any other versions.

Community
  • 1
  • 1
Adam Schreiner
  • 504
  • 2
  • 6
6

In general, it is recommended to have only one version of dependency in your classpath at a given time. Doing in such a way will allow you to know exactly what version of class will be used at runtime.

To avoid version conflicts try to specify your dependency like this:

<dependency>
    <groupId>commons-daemon</groupId>
    <artifactId>commons-daemon</artifactId>
    <version>1.0.1</version>
    <exclusions>
        <exclusion>
            <groupId>some_group</groupId>
            <artifactId>some_artifact</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Where you need to specify the groupId and artifactId of your conflicting artifact with version 2.5.

Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
  • 5
    This doesn't answer the question. – Flaom Dec 14 '20 at 18:59
  • 5
    This is partial answer , its only solve how to exclude dependency from parent , don't include how to include new version to the child dependency . – Appesh May 09 '21 at 01:49