andrew.z
andrew.z

Reputation: 1059

Resolve maven transitive dependency conflict

My project depends on a thirdparty library, the dependency is defined in my POM like this:

<dependency>
    <groupId>thirdparty</groupId>
    <artifactId>main</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

This thirdparty main library in turn depends on other two libraries, here's a part of dependency management defined in its pom:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>thirdparty</groupId>
            <artifactId>x</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>thirdparty</groupId>
            <artifactId>y</artifactId>
            <version>1.0.0</version>
        </dependency>
        ...

Now the thirdparty x library has a dependency on y defined in its pom like this:

<dependency>
    <groupId>thirdparty</groupId>
    <artifactId>y</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

Note the snapshot version! This looks like a problem in thirdparty poms, but I have no control over it.

The interesting thing though is that if you try to maven build the main thirdparty project it uses (resolves and installs to local repo) the correct thirdparty:y:1.0.0 version of artifact. But when I'm building my original project it tries to resolve the snapshot version of thirdparty:y.

My questions are:

  1. Why does this happen? I was sure that maven should choose the artifact version that is found closest to the project root, which would be 1.0.0 in my case.

  2. Is there any way to fix this problem without adding explicit dependencies to thirdparty:y:1.0.0 to my project's pom?

Upvotes: 1

Views: 784

Answers (1)

stefaan dutry
stefaan dutry

Reputation: 1106

First of all make sure you realy need the snapshot version. There should normaly be a released version (without -SNAPSHOT).

if you do need it, this should do the trick:

<properties>
    <dependeny.main.version>1.0.0-SNAPSHOT</dependeny.main.version>
    <dependeny.x.version>1.0.0</dependeny.x.version>
    <dependeny.y.version>1.0.0</dependeny.y.version>
<properties>
<dependencies>
    ...
    <dependency>
        <groupId>thirdparty</groupId>
        <artifactId>main</artifactId>
    </dependency>
    <dependency>
        <groupId>thirdparty</groupId>
        <artifactId>x</artifactId>
    </dependency>
    <dependency>
        <groupId>thirdparty</groupId>
        <artifactId>y</artifactId>
    </dependency>
    ...
</dependencies>

<dependencyManagement>
    <dependencies>
        ...
        <dependency>
            <groupId>thirdparty</groupId>
            <artifactId>main</artifactId>
            <version>${dependeny.main.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>thirdparty</groupId>
                    <artifactId>x</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>thirdparty</groupId>
                    <artifactId>y</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>thirdparty</groupId>
            <artifactId>x</artifactId>
            <version>${dependeny.x.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>thirdparty</groupId>
                    <artifactId>y</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>thirdparty</groupId>
            <artifactId>y</artifactId>
            <version>${dependeny.y.version}</version>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>

I hope this helps you out.

Upvotes: 0

Related Questions