简体   繁体   中英

Wrong dependencies in IntelliJ when importing project from Gradle

I have a project with compile dependencies on Swagger which brings jackson-databind v2.4.5 and a testCompile dependency on a library that makes use of AWS SDK which brings jackson-databind v2.6.6.

When running a test from gradle, everything works fine and the correct updated jackson dependency v2.6.6 is on classpath:

+--- io.swagger:swagger-jersey2-jaxrs:1.5.10
|    +--- io.swagger:swagger-jaxrs:1.5.10
|    |    +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.4.5
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.4.5 -> 2.6.6
|    |    |    \--- org.yaml:snakeyaml:1.12 -> 1.13
|    |    +--- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.4.5
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.4.5 -> 2.6.6
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.4.5 -> 2.6.6

The intellij dependencies contain both versions:

显示两个依赖项

However when running tests from IntelliJ, IntelliJ creates a "classpath jar" ( because the classpath is too long). When inspecting the classpath in the META-INF/MANIFEST.MF, I can see that only the smaller version jar (2.4.5) is included.

This causes an exception when the tests use the AWS SDK:

java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;

Because the older jackson API did not contain this method.

Is this somehow preventable? I am currently using a workaround where I added the version 2.6.6 of the jackson-databind dependency in the testCompile dependencies. But I would like to avoid this, because

  1. We are not using jackson directly, it's in the gradle script just because of intellij
  2. every time the library that is using the AWS SDK changes, I'll have to change the jackson dependency as well

Note that we use IntelliJ test runner simply because it's faster than running tests through gradle on local workstations (mainly due to incremental compilation).

Can't say, why IDE still has 2 versions of this library, though Gradle resolved dependencies. You can try to manually exclude transitive dependency on jackson-databind v2.4.5, something like this:

compile('io.swagger:swagger-jersey2-jaxrs:1.5.10') {
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
}

And then synchronize Gradle and IDEA projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM