user139386
user139386

Reputation: 71

How do I resolve dependency conflict for a Java file in IntelliJ IDEA?

I have tried to search web for the problem I am facing but maybe I am not asking google the right question so here I am.

I am using IntelliJ IDEA for my multi-module project. For one of my modules, one of the class file is using a static import -

import static javax.ws.rs.core.Response.Status.Family.familyOf;

Being a big project, there are a lot of dependencies downloaded from internal repo but for some reason IntelliJ refuses to use the dependency "javax.ws.rs-api-2.0" instead it is using "jersey-core-1.8". Because of this it is throwing a compilation error saying Cannot find symbol "familyof".

I looked into Response.java from both the dependencies and found that jersey dependency does not have familyof method while javax.ws.rs-api-2.0 has it but IntelliJ doesnt use this dependency. How do I fix this problem. Most of the developers in my team are using Eclipse and they do not have this problem. I am trying to get used to IntelliJ IDE but cant seem to figure a way out of this. Any help in this regard is much appreciated.

PS - This issue does not occur in Eclipse IDE.

Upvotes: 1

Views: 16624

Answers (3)

Sébastien Temprado
Sébastien Temprado

Reputation: 1463

How does Maven choose between two versions of a same dependency?

As explained here, Maven chooses the first met dependency that's why it works when you change the order of the dependency in the pom.

How to find Maven dependencies conflicts?

In a terminal, in the pom folder :

mvn dependency:tree -Dverbose | grep "conflict"

will give you all the dependencies in conflict in your project.

With Eclipse IDE, click on the pom and on the Dependency hierarchy tab. Then, fill the Filter field with a dependency. On the left side, you will see the conflicts (like with mvn dependendy:tree, with filtered results) and on the right side, the dependencies chosen.

With IntelliJ, the documentation of IntelliJ can help you. There is a diagram view to find the conflicts.

How to resolve Maven dependencies conflicts?

  1. Add dependencyManagement tag in the pom to tell Maven which dependency you want

Example :

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
    <dependencies>
</dependencyManagement>
  1. Change the order of the dependency in the pom
  2. Add an exclusion of the dependency

Example :

    <dependency>
        <groupId>com.my.groupid</groupId>
        <artifactId>my-artifact-id</artifactId>
        <version>1.2.2</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Why is there a difference between IntelliJ and Eclipse?

Please, read the documentation of IntelliJ to configure the project dependencies easily.

Upvotes: 2

Vincent F
Vincent F

Reputation: 7341

You may be able to get the right result every time with Maven by shuffling around the dependencies in the pom.xml, and make sure the dependency you want to take precedence is declared first in the list of dependencies. yes, the order in which the dependencies are declared in pom.xml matters !

Then, if all of you are using the same Maven version, you should have a consistent result.

Upvotes: 2

user139386
user139386

Reputation: 71

I could resolve this issue by following the below mentioned steps -

  1. Goto "Open Module Settings" Command+Down arrow key
  2. Select Dependencies tab
  3. Search for the above two dependencies in the list
  4. Move "javax.ws.rs-api-2.0" dependency up to ensure this dependency is above "jersey-core-1.8" dependency.

I don't think this is a permanent solution but it seemed to work. if someone with in-depth knowledge of Java/Mave/IntelliJ has an answer to this question that would be great!

Upvotes: 1

Related Questions