1

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.

Sébastien Temprado
  • 1,413
  • 4
  • 18
  • 29
user139386
  • 61
  • 1
  • 1
  • 9
  • 1
    You and your teammates should be using maven and a POM. In which case your dependencies would be automagically managed for you and this situation would not arise. If you're using the ide to manage the dependencies, then you must use the gui to remove the javax.ws.rs-api dependency and add a dependency on jersey-core. – Software Engineer Jul 22 '16 at 16:42
  • We use maven and a POM. The code base is hosted on company's internal github repo so I am wondering why would it work for everyone else and not for me. I bet i am missing some intelliJ setting which I cant figure out. If someone is using Eclipse and they dont see this problem, why would it happen when I use IntelliJ? – user139386 Jul 22 '16 at 16:53

3 Answers3

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!

user139386
  • 61
  • 1
  • 1
  • 9
  • 2
    it does not work by this way, you need to change dependency in pom file. put library you want in higher position. that work – uncle bob Feb 15 '20 at 15:36
1

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.

Vincent F
  • 6,523
  • 7
  • 37
  • 79
1

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.

Sébastien Temprado
  • 1,413
  • 4
  • 18
  • 29