【Maven Helper】分析依赖冲突案例

A must have plugin for working with Maven.

  • easy way for analyzing and excluding conflicting dependencies
  • actions to run/debug maven goals for a module that contains the current file or on the root module
  • action to open terminal at the current maven module path
  • actions to run/debug the current test file.
    Usage:
  • Right click in Editor | Run Maven
  • Right click in Project View Toolbar | (Run|Debug) Maven
  • CTRL + ALT + R - "Run Maven Goal" popup (you can use Delete key in the popup)
  • CTRL + SHIFT + ALT + R - "Run Maven Goal on Root Module" popup (you can use Delete key in the popup)
  • Customize goals: Settings | Other Settings | Maven Helper
  • Define shortcuts: Settings | Keymap | Plug-ins | Maven Helper
  • Open pom file, click on 'Dependency Analyzer' tab, right click in the tree for context actions.

实际案例

java文件

java 复制代码
package org.example;

import com.alibaba.excel.EasyExcel;

import java.util.ArrayList;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        test();

    }

    public static void test() {
        //1.获取文件地址
        String fileName = "/excel/test.xlsx";
        //2、调用easyExcel里面的方法实现写操作
        EasyExcel.write(fileName, Object.class).sheet("某某报表").doWrite(new ArrayList<>());
    }
}

pom.xml文件

xml 复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>MavenHelperDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenHelperDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.10.1</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>2.2.6</version>
    </dependency>
  </dependencies>
</project>

主要依赖的为poi-ooxml 3.10.1版本和easyexcel 2.2.6版本。

运行抛出异常

bash 复制代码
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/TempFileCreationStrategy
	at com.alibaba.excel.write.ExcelBuilderImpl.<clinit>(ExcelBuilderImpl.java:30)
	at com.alibaba.excel.ExcelWriter.<init>(ExcelWriter.java:47)
	at com.alibaba.excel.write.builder.ExcelWriterBuilder.build(ExcelWriterBuilder.java:130)
	at com.alibaba.excel.write.builder.ExcelWriterBuilder.sheet(ExcelWriterBuilder.java:146)
	at com.alibaba.excel.write.builder.ExcelWriterBuilder.sheet(ExcelWriterBuilder.java:142)
	at org.example.App.test(App.java:24)
	at org.example.App.main(App.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.util.TempFileCreationStrategy
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 7 more

分析

  1. 从ExcelBuilderImpl的30行抛出异常,java.lang.NoClassDefFoundError: org/apache/poi/util/TempFileCreationStrategy。知道依赖的jar包版本种缺少该类。
  2. 打开Maven视图,可以知道easyexcel 2.2.6依赖的poi poi-ooxml poi-ooxml-schemas版本原本是3.17,由于依赖冲突,现在的依赖的版本都是3.10.1
  3. 打开pom文件的选择 Dependency Analyzer视图,选择Conflicts,可以发现冲突的jar有poi poi-ooxml poi-ooxml-schemas slf4j-api。从下图所示右侧可以看到easyexcel 2.2.6依赖的1.7.26版本的slf4j-api 与 easyexcel 2.2.6依赖 =>3.4.0版本的ehcache=>1.7.7的slf4j-api冲突,由依赖冲突解决原则,最终选择了1.7.26版本的slf4j-api。同理可以分析另外3个冲突
  4. org/apache/poi/util/TempFileCreationStrategy类是在poi包中的,所以我们现在要做的就是将poi的版本调整到3.17,所以我们可以通过exclude的方式

    6 . Maven视图查看,就可以看见poi的冲突没有了

参考资料

解决maven依赖冲突,这篇就够了!

相关推荐
wjm041006几秒前
C++的四种类型转换
java·开发语言·c++
小园子的小菜34 分钟前
深入探究 RocketMQ 中的 Broker2Client 组件
java·rocketmq·java-rocketmq
bing_15844 分钟前
Mybatis 如何自定义缓存?
java·缓存·mybatis
RainbowSea1 小时前
8. RabbitMQ 消息队列 + 结合配合 Spring Boot 框架实现 “发布确认” 的功能
java·消息队列·rabbitmq
RainbowSea1 小时前
7. RabbitMQ 消息队列——延时队列(Spring Boot + 安装message_exchange"延迟插件" 的详细配置说明)的详细讲解
java·消息队列·rabbitmq
XuanXu2 小时前
Java volatile关键字以及线程安全
java
都叫我大帅哥2 小时前
代码世界的「万能转接头」:适配器模式的跨界艺术
java·后端·设计模式
蝈蝈噶蝈蝈噶2 小时前
问题:tomcat下部署eureka双重路径
java·eureka·tomcat
禹曦a2 小时前
Java进阶之旅-day05:网络编程
java·开发语言·网络
五行星辰2 小时前
Gson修仙指南:谷歌大法的佛系JSON渡劫手册
java·后端