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

相关推荐
苹果酱05673 小时前
Golang的文件加密技术研究与应用
java·vue.js·spring boot·mysql·课程设计
xweiran5 小时前
CAS操作的底层原理(总线锁定机制和缓存锁定机制 )
java·cas·处理器·总线锁定·缓存锁定
Miraitowa_cheems5 小时前
[JavaEE] Spring IoC&DI
java·spring·java-ee
V+zmm101345 小时前
基于微信小程序的水果销售系统的设计与实现springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·springboot
头发那是一根不剩了5 小时前
java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
java
小白起 v5 小时前
三天学完微服务其二
java·微服务·架构
huiyunfei6 小时前
MinorGC FullGC
java·jvm·算法
XWM_Web6 小时前
JavaAPI.02.包装类与正则表达式
java·开发语言·学习·eclipse
PangPiLoLo6 小时前
架构学习——互联网常用架构模板
java·学习·微服务·云原生·架构·系统架构·nosql
!!!5256 小时前
SpringBoot-web入门程序剖析
java·spring boot·后端