之前在做项目的时候需要在 SpringBoot中引入 ElasticSearch,但是由于服务器版本较低,最新版 ES 是6.x.x,而我需要的是5.1.1(org.elasticsearch.client:transport:5.1.1
)。当我引入5.1.1的版本包的时候,org.elasticsearch.client:transport:5.1.1
中依赖的org.elasticsearch:elasticsearch
自动升级成了6.x.x。 折腾搜索了一天,终于找到了原因。在这里记录一下整个排查流程。
一开始在启动SpringBoot
时,会抛出 ElasticSearch 有某个类/某个方法未找到的异常,因此怀疑是引入的包版本不正确。
使用 gradle -q dependencies
指令,输出如下
+--- org.elasticsearch.client:transport:5.1.1
| +--- org.elasticsearch:elasticsearch:5.1.1 -> 6.4.3
| | +--- org.elasticsearch:elasticsearch-core:6.4.3
| | +--- org.elasticsearch:elasticsearch-secure-sm:6.4.3
| | +--- org.elasticsearch:elasticsearch-x-content:6.4.3
| | | +--- org.elasticsearch:elasticsearch-core:6.4.3
| | | +--- org.yaml:snakeyaml:1.17 -> 1.23
| | | +--- com.fasterxml.jackson.core:jackson-core:2.8.10 -> 2.9.8
| | | +--- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:2.8.10 -> 2.9.8
| | | +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.10 -> 2.9.8 (*)
| | | \--- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.8.10 -> 2.9.8
| | +--- org.apache.lucene:lucene-core:7.4.0
| | +--- org.apache.lucene:lucene-analyzers-common:7.4.0
| | +--- org.apache.lucene:lucene-backward-codecs:7.4.0
| | +--- org.apache.lucene:lucene-grouping:7.4.0
| | +--- org.apache.lucene:lucene-highlighter:7.4.0
| | +--- org.apache.lucene:lucene-join:7.4.0
| | +--- org.apache.lucene:lucene-memory:7.4.0
| | +--- org.apache.lucene:lucene-misc:7.4.0
| | +--- org.apache.lucene:lucene-queries:7.4.0
| | +--- org.apache.lucene:lucene-queryparser:7.4.0
| | +--- org.apache.lucene:lucene-sandbox:7.4.0
| | +--- org.apache.lucene:lucene-spatial:7.4.0
| | +--- org.apache.lucene:lucene-spatial-extras:7.4.0
| | +--- org.apache.lucene:lucene-spatial3d:7.4.0
| | +--- org.apache.lucene:lucene-suggest:7.4.0
| +--- org.elasticsearch.plugin:transport-netty4-client:5.1.1 -> 6.4.3
| | +--- io.netty:netty-buffer:4.1.16.Final -> 4.1.33.Final (*)
| | +--- io.netty:netty-codec:4.1.16.Final -> 4.1.33.Final (*)
| | +--- io.netty:netty-codec-http:4.1.16.Final -> 4.1.33.Final
| | +--- io.netty:netty-common:4.1.16.Final -> 4.1.33.Final
| | +--- io.netty:netty-handler:4.1.16.Final -> 4.1.33.Final (*)
| | +--- io.netty:netty-resolver:4.1.16.Final -> 4.1.33.Final (*)
| | \--- io.netty:netty-transport:4.1.16.Final -> 4.1.33.Final (*)
可以看到transport下所依赖的部分包自动升级成了6.4.3。但是在gradle中并没有设置什么。 因此继续使用gradlew -q dependencyInsight --dependency elasticsearch --info
查看 elasticsearch的具体依赖:
org.elasticsearch:elasticsearch:6.4.3 (selected by rule)
variant "runtime" [
org.gradle.status = release (not requested)
Requested attributes not found in the selected variant:
org.gradle.usage = java-api
]
org.elasticsearch:elasticsearch:5.1.1 -> 6.4.3
\--- org.elasticsearch.client:transport:5.1.1
\--- compileClasspath
org.elasticsearch:elasticsearch-cli:6.4.3
variant "runtime" [
org.gradle.status = release (not requested)
Requested attributes not found in the selected variant:
org.gradle.usage = java-api
]
org.elasticsearch:elasticsearch-cli:6.4.3
\--- org.elasticsearch:elasticsearch:6.4.3
\--- org.elasticsearch.client:transport:5.1.1
\--- compileClasspath
org.elasticsearch:elasticsearch-core:6.4.3
variant "runtime" [
org.gradle.status = release (not requested)
Requested attributes not found in the selected variant:
org.gradle.usage = java-api
]
org.elasticsearch:elasticsearch-core:6.4.3
+--- org.elasticsearch:elasticsearch:6.4.3
| \--- org.elasticsearch.client:transport:5.1.1
| \--- compileClasspath
+--- org.elasticsearch:elasticsearch-cli:6.4.3
| \--- org.elasticsearch:elasticsearch:6.4.3 (*)
\--- org.elasticsearch:elasticsearch-x-content:6.4.3
\--- org.elasticsearch:elasticsearch:6.4.3 (*)
发现了前两项直接使用了6.4.3的版本,给出的理由是 **selected by rule**
,我就想说gradle这是啥奇奇怪怪的规则。。。经过一番搜索,终于发现这一篇帖子
大体是说SpringBoot
的插件,会通过Spring dependency-management-plugin
增加自己的依赖规则,其中包括会强制引入特定的版本,并忽视掉自己在gradle配置的版本(这也太坑了吧)。
//就是这个插件
apply plugin: 'io.spring.dependency-management'
所以最后的解决方法是自定义全局的版本控制,如下
allprojects {
configurations.all {
resolutionStrategy {
dependencySubstitution {
substitute module('org.elasticsearch:elasticsearch') with module('org.elasticsearch:elasticsearch:5.1.1')
substitute module('org.elasticsearch.plugin:transport-netty4-client') with module('org.elasticsearch.plugin:transport-netty4-client:5.1.1')
substitute module('org.elasticsearch.client:elasticsearch-rest-client') with module('org.elasticsearch.client:elasticsearch-rest-client:5.1.1')
}
}
}
}
这可以告诉gradle
强制使用5.1.1版本去替换依赖的elasticsearch的版本。当然这里我引入的transport
所依赖的transport-netty4-client
和elasticsearch-rest-client
都被强制升级了,所以这里也进行配置一下。
最后,也许可以通过配置spring的插件解决,附上插件的github地址,有空研究一下