How to determine where is the version comes from in a mvn project?

Viewed 868

For a mvn project, we may use dependencymanagement to do version control. But the problem also come up, like, The project's parent pom:

<parent>
    <groupId>com.XXX</groupId>
    <artifactId>XXX-dependencies</artifactId>
    <version>1.0.0</version>
</parent>

<dependencyManagement>
        <dependency>
            <groupId>com.YYY</groupId>
            <artifactId>YYY-dependencies</artifactId>
            <version>1.1.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
</dependencyManagement>

if there is a jar, named com.AAA:BBB, is controlled both in com.XXX:XXX-dependencies and com.YYY:YYY-dependencies, how can we know which version maven will use finally ? Is there a tool to help us to identify ? I have tried mvn dependency plugin, mvn help plugin and dependency analyzer in idea, no one works.

2 Answers

Think in a different way. I guess you don't have to find out which version is getting from the other dependencies (transitive). You just need to make sure the version of AAA:BBB is coming in the project as expected.

Just add com.AAA:BBB (required dependency) in your pom.xml as an explicit dependency with your required version. So that it should look like below code

<parent>
    <groupId>com.XXX</groupId>
    <artifactId>XXX-dependencies</artifactId>
    <version>1.0.0</version>
</parent>

<dependencyManagement>
        <dependency>
            <groupId>com.YYY</groupId>
            <artifactId>YYY-dependencies</artifactId>
            <version>1.1.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.AAA</groupId>
            <artifactId>AAA-dependencies</artifactId>
            <version>BBB</version> <!-- The version you expect -->
            <type>pom or jar whatever</type>
            <scope>import</scope>
        </dependency>
</dependencyManagement>

Make sure the two libraries you are consuming are runtime compatible with that version.

There are some rules that maven follow to resolve transitive dependencies. First is

A.v1
- B.v1
   - D.v1
- C.v1
   - E.v1
     - D.v2

Above D.v1 will be consumed as the nearest dependency to D is A-B-D.

A.v1
- B.v1
   - D.v1
- C.v1
   - D.v2

In above, as per nearest dependency rule both are at same level, so now the dependency included (written) first will be consumed, ie. D.v1.

Related