0
votes

Could someone explain how do people get duplicate dependencies with different versions in their war? Maybe give some examples

I cannot understand for the live of me. Maven2 has dependency mediation for transitive dependencies

Say I have:

A
- B
- C

B
- E

Now in a project X, with war packaging I add A and B as dependency.

Because of the aforementioned dependency mediation, regardless of the version of B from project A, in the war I will see a single B jar with the declared version in X.

This because maven will use the version of the closest dependency to my project.

So, what am I missing here? How do people mess this up? Looking forward to enlightenment

1
Just to be clear, the same thing happens with transitive dependencies further along the dependency tree. The closest one to the project is chosenCapp Argo
Dunno about WARs but I have seen people getting duplicated dependencies in EARs easily.Hubert Grzeskowiak

1 Answers

0
votes

For conflicting jars with same groupId and artifactId , maven takes the version coming from the dependency declared at last .

You can play with dependency order. Just change the dependency order of A and B in project X .

Scenario 1 : Here since 2.5 is added after 2.3 , 2.5 wil be packaged.

<dependencies>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.3</version>

        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.5</version>

        </dependency>

    </dependencies>

Scenario 2 : Here since 2.3 is added after 2.5 , 2.3 wil be packaged.

<dependencies>

            <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.5</version>

        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.3</version>

        </dependency>

    </dependencies>

You can also try exclusion concept here.