目录
- Maven Helper
- 实际案例
- java文件
- pom.xml文件
- 运行抛出异常
- 分析
- 参考资料
骆宾王是在自己7岁的时候就写下了这首杂言
Maven Helper
A must have plugin for working with Maven. 一个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文件
package org.example;import com.alibaba.excel.EasyExcel;import java.util.ArrayList;
import java.util.Date;/*** Hello world!**/
public class App
{public static void main( String[] args ){System.out.println( "Hello World!" );test();}public static void test() {ArrayList<DemoData> demoDataArrayList = new ArrayList<>();DemoData demoData = new DemoData();demoData.setString("骆宾王");demoData.setDoubleData(1.0);demoData.setDate(new Date());demoDataArrayList.add(demoData);//1.获取文件地址String fileName = "./test.xlsx";//2、调用easyExcel里面的方法实现写操作EasyExcel.write(fileName, DemoData.class).sheet("模拟数据报表").doWrite(demoDataArrayList);}
}
pom.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版本。
运行抛出异常
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/TempFileCreationStrategyat 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.TempFileCreationStrategyat 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
分析
-
从ExcelBuilderImpl的30行抛出异常,java.lang.NoClassDefFoundError: org/apache/poi/util/TempFileCreationStrategy。知道依赖的jar包版本种缺少该类。通过 ExcelBuilderImpl的30行跳转到相应的代码,你可以发现其实编辑器已经报错了,如下图所示
-
打开Maven视图,可以知道easyexcel 2.2.6依赖的poi poi-ooxml poi-ooxml-schemas版本原本是3.17,由于依赖冲突,现在的依赖的版本都是3.10.1
-
打开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个冲突
-
org/apache/poi/util/TempFileCreationStrategy类是在poi包中的,所以我们现在要做的就是将poi的版本调整到3.17,所以我们可以通过exclude的方式
6 . Maven视图查看,就可以看见poi的冲突没有了
参考资料
解决maven依赖冲突,这篇就够了!