18

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

 3 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzIwMDY0Nzk2Mw%3D%3D&%3Bmid=2650321566&%3Bidx=1&%3Bsn=94480c08ac8808d9c563192df7cdc121
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

项目推荐

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

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

问题背景

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

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

VjEBnqZ.png!web

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

EN3UVz6.png!web

问题原因

这么看来,应该是我的项目中哪里已经有了版本的限制,覆盖了 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 的依赖关系:

aUbIjuA.png!web

问题解决

在使用项目的 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 即可。

关于作者 :尹吉欢,简单的技术爱好者,《Spring Cloud 微服务-全栈技术与案例解析》, 《Spring Cloud 微服务 入门 实战与进阶》作者, 公众号 猿天地 发起人。个人微信 jihuan900 ,欢迎勾搭

相关推荐

后台回复 学习资料   领取学习视频

YnaUVjb.jpg!web

如有收获,点个在看,诚挚感谢


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK