简体   繁体   English

从Gradle导入项目时,IntelliJ中的依赖项错误

[英]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. 我有一个带有Swagger编译依赖项的项目,它带来了jackson-databind v2.4.5和一个testCompile依赖项,该库使用了带有jackson-databind v2.6.6的AWS SDK。

When running a test from gradle, everything works fine and the correct updated jackson dependency v2.6.6 is on classpath: 从gradle运行测试时,一切正常,并且正确更新的jackson依赖v2.6.6在类路径上:

+--- 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: intellij依赖包含两个版本:

显示两个依赖项

However when running tests from IntelliJ, IntelliJ creates a "classpath jar" ( because the classpath is too long). 但是,当从IntelliJ运行测试时,IntelliJ会创建一个“类路径jar”(因为类路径太长)。 When inspecting the classpath in the META-INF/MANIFEST.MF, I can see that only the smaller version jar (2.4.5) is included. 在检查META-INF / MANIFEST.MF中的类路径时,我可以看到只包含较小版本的jar(2.4.5)。

This causes an exception when the tests use the AWS SDK: 当测试使用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. 因为较旧的jackson API不包含此方法。

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. 我目前正在使用一种解决方法,我在testCompile依赖项中添加了jackson-databind依赖项的2.6.6版本。 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 我们没有直接使用jackson,因为intellij,它在gradle脚本中
  2. every time the library that is using the AWS SDK changes, I'll have to change the jackson dependency as well 每次使用AWS SDK的库发生更改时,我都必须更改jackson依赖项

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). 请注意,我们使用IntelliJ测试运行器只是因为它比通过本地工作站上的gradle运行测试更快(主要是由于增量编译)。

Can't say, why IDE still has 2 versions of this library, though Gradle resolved dependencies. 不能说,为什么IDE仍然有这个库的2个版本,尽管Gradle解决了依赖关系。 You can try to manually exclude transitive dependency on jackson-databind v2.4.5, something like this: 您可以尝试手动排除对jackson-databind v2.4.5的传递依赖,如下所示:

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. 然后同步Gradle和IDEA项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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