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

蹲街弑〆低调 提交于 2021-02-11 07:10:55

问题


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.


回答1:


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!




回答2:


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.




回答3:


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?

  • You can add a dependency to a module without adding it to the pom as explained in the documentation of IntelliJ. It's probably possible with other IDE.

    IntelliJ IDEA lets you add a Maven dependency to your project. We recommend that you specify the dependency inside your POM. Dependencies that you set up manually inside IntelliJ IDEA module settings will be discarded on the next Maven project import.

  • The Maven plugin in Eclipse (M2Eclipse) may load dependencies differently compared to mvn

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



来源:https://stackoverflow.com/questions/38530745/how-do-i-resolve-dependency-conflict-for-a-java-file-in-intellij-idea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!