15

I have some test code in Intellij Idea 2018 community edition, which has multiple pom files. When I run any testng annotated test, I get an error which says "no tests were found". It looks like the problem is due to this part of the exception stack trace:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge

I googled for a solution and found this - https://github.com/FasterXML/jackson-annotations/issues/119 and this https://stackoverflow.com/a/46406070. It looks like this issue is caused when we don't have the same version of these jars in the project.

1) jackson-core (2.8.8)

2) jackson-databind (2.9.2)

3) jackson-annotations (2.8.5)

As you can see, I don't have the same version for all the jars. I looked at all the poms in my project and did not find any place where all these dependencies are added. I was hoping to simply set the version number there. Should I simply add all dependencies in my parent pom file or do something else ?

How do I resolve this issue without harming the project ? How do I find out why these jars are not of the same version ?

MasterJoe
  • 2,103
  • 5
  • 32
  • 58

2 Answers2

10

You most likely have different versions imported through different dependencies as sub-dependencies.

You can get maven to show you the so-called "effective pom" which will give you the full dependency tree, from which you can then see where what's included.

Some IDEs (like IntelliJ) have an option to show this graphically, which makes finding conflicts like this a lot easier.

Exclude lower versions, and if required explicitly add dependencies to newer versions.

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • Thanks. How do I see the "effective pom" graphically in intellij idea ? Also, how do I exclude lower versions or explicitly add dependencies to newer versions ? – MasterJoe Aug 27 '18 at 20:21
  • 1
    Open the pom in the editor or select it in the maven projects view, right click->Maven->show dependencies. May not work in the community edition. To exclude things, just add an explicit exclude for it. To include other versions, just add them as dependencies themselves. – jwenting Aug 28 '18 at 04:48
  • Yes, the option is not there in the community edition. I'll try to import it into eclipse and use its dependency view features. Btw, I'd like to learn how to resolve such issues in a proper way. Could you please suggest some links for that or suggest some google search words to get such links ? Thanks. – MasterJoe Aug 29 '18 at 17:17
  • 2
    You can also just use maven as a command line tool and run "mvn help:effective-pom" in your project directory. Also, you could use "mvn dependency:tree -Dverbose -Dincludes=commons-collections" to get a full overview where your dependencies stem from. – mbruns42 Aug 30 '18 at 09:34
8

The keyword you are looking for is "Dependency Exclusion". Maven includes transitive dependencies automatically. You first need to identify where the dependencies are coming from.

You can redirect the output to a file and analyze it in detail by searching for "jackson" in the tree.txt file generated as follows:

mvn dependency:tree -Dverbose > tree.txt

Next step would be find out whether you can upgrade some of your libraries so that they automatically use the right version of jackson libraries for you.

Finally, if you explicitly want to exclude transitive dependencies, you can use <exclusions> tag inside a certain <dependency> to exclude certain third party dependencies added to your classpath. See this SO question, for example.

Jaywalker
  • 3,079
  • 3
  • 28
  • 44