前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >这个Maven依赖的问题,你敢说你没遇到过

这个Maven依赖的问题,你敢说你没遇到过

作者头像
猿天地
发布2020-06-04 11:29:11
6280
发布2020-06-04 11:29:11
举报
文章被收录于专栏:猿天地猿天地猿天地

项目推荐

https://github.com/yinjihuan/kitty-cloud

Maven 依赖没处理好的话经常会导致发生一些问题,非常烦。今天给大家分享一个依赖相关的问题,说不定你之前就遇到过。

问题背景

有个 ES 搜索的项目,刚开始还是好好的状态,过了一段时间,然后就发现启动时报错了。看了下 Git 提交日志,也没有改动过,神奇的代码世界。

错误如下图所示,很明显的错误,经常会碰到,肯定是依赖版本的问题。

kitty-spring-cloud-starter-elasticsearch 是我自己封装的,里面用的版本是 6.8.7。最开始测试的时候也是正常的,这突然就不行了。我看了下目前项目的依赖,发现变成了 6.4.3。所以才找不到 CountRequest 这个类。

问题原因

这么看来,应该是我的项目中哪里已经有了版本的限制,覆盖了 kitty-spring-cloud-starter-elasticsearch 定义的 6.8.7 版本。

在项目的父 pom 中也没找到对应的配置,唯一有可能的就是 Spring Boot 中了。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath />
</parent>

在 spring-boot-dependencies 中找到了 6.4.3 的配置。

<properties>
  <elasticsearch.version>6.4.3</elasticsearch.version>
<properties>
  
<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>${elasticsearch.version}</version>
</dependency>
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>${elasticsearch.version}</version>
</dependency>
<dependency>
  <groupId>org.elasticsearch.distribution.integ-test-zip</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>${elasticsearch.version}</version>
  <type>zip</type>
</dependency>
<dependency>
  <groupId>org.elasticsearch.plugin</groupId>
  <artifactId>transport-netty4-client</artifactId>
  <version>${elasticsearch.version}</version>
</dependency>
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-client</artifactId>
  <version>${elasticsearch.version}</version>
  <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-high-level-client</artifactId>
  <version>${elasticsearch.version}</version>
</dependency>

我们都知道如果父 pom 中 dependencyManagement 定义了版本的话,子模块中可以不用指定版本,直接依赖父 pom 的版本,我们这里就是因为没有强制指定,所以用了最顶层父 pom 定义的版本。

下图就是我们 pom 的依赖关系:

问题解决

在使用项目的 pom 中直接定义版本,优先级高于父 pom 的定义,这样才可以强制使用我们需要的版本。只能在 kitty-cloud-search.pom 中定义才可以。

<dependency>
    <groupId>com.cxytiandi</groupId>
    <artifactId>kitty-spring-cloud-starter-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <groupId>org.elasticsearch.client</groupId>
        </exclusion>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.8.7</version>
    <exclusions>
        <exclusion>
            <artifactId>elasticsearch-rest-client</artifactId>
            <groupId>org.elasticsearch.client</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>6.8.7</version>
</dependency>

这样做能解决问题,始终感觉不是很好。

如果想规避这个问题,除非说我们就用 spring boot 已经定义好了的版本,这样就是一致的了,但总会有一些特殊的需求嘛,虽然你定义了在 2.1.6.RELEASE 版本的 Spring Boot 中 ES 就用 6.4.3,但是我还是想用其他版本的应该也挺常见。

还有一种方式就是如果是公司内部是统一的开发框架的话,你自己可以定义 dependencies 来管理框架的版本,直接把 spring-boot-dependencies 的内容复制一份,然后改掉 elasticsearch.version 的值,最后在项目中就直接用你自定义的 dependencies 即可。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 猿天地 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目推荐
  • 问题背景
  • 问题原因
  • 问题解决
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档