【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依赖冲突,这篇就够了!

相关推荐
爱吃烤鸡翅的酸菜鱼4 分钟前
Java算法OJ(11)双指针练习
java·数据结构·算法·leetcode
麒麟而非淇淋12 分钟前
Day3 苍穹外卖项目 公共字段自动填充(AOP)、文件上传、新增菜品、菜品分页查询、删除菜品、修改菜品
java·mybatis
Gary董13 分钟前
ArraList和LinkedList区别
java
一到破伤风14 分钟前
Java对象与XML互相转换(xstream)
xml·java·服务器
Dingdangr18 分钟前
ArrayList与LinkedList的区别是什么?
java
源码宝24 分钟前
HIS系统源代码
java·人工智能·源码·his系统·程序代码
SUN_Gyq32 分钟前
在 C/C++ 中,volatile 关键字的作用是什么?volatile 关键字与 const 关键字有什么区别?
java·开发语言·jvm·c++
极客先躯44 分钟前
liteflow 架构详解
java·linux·运维·spring·架构·springboot·liteflow
哆啦 AI 梦1 小时前
【Maven】项目创建
maven
oscar9991 小时前
JDK的版本演化,JDK要收费吗?
java·开发语言