记一次解决jar包版本冲突

  • 背景

当时是在将Excel导出从POI转成使用EasyExcel

POI使用的maven坐标如下:


  org.apache.poi
  poi
  3.8


  org.apache.poi
  poi-ooxml
  3.8

EasyExcel添加的maven坐标如下:


	com.alibaba
	easyexcel
	1.1.2-beat1

编写好代码debug启动项目,启动没任何报错,在执行改方法时,控制台报错了,报错信息如下:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Font.setBold(Z)V
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:978)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167)
	at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Font.setBold(Z)V
	at com.alibaba.excel.util.StyleUtil.buildDefaultCellStyle(StyleUtil.java:22)
	at com.alibaba.excel.context.WriteContext.(WriteContext.java:99)
	at com.alibaba.excel.write.ExcelBuilderImpl.(ExcelBuilderImpl.java:38)
	at com.alibaba.excel.ExcelWriter.(ExcelWriter.java:58)
	at com.alibaba.excel.ExcelWriter.(ExcelWriter.java:34)
	at com.wyj.controller.EasyExcelDemo.findAllExcelByEasyExcel(EasyExcelDemo.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
	... 47 common frames omitted
  • 报错信息大概的信息是说
    记一次解决jar包版本冲突_第1张图片
    在at com.alibaba.excel.util.StyleUtil类中的buildDefaultCellStyle方法中没找到setBold(Z)V这个方法,点进StyleUtil这个类的源码中,找到setBold(Z)V这个方法,在这里调用了org.apache.poi.ss.usermodel.Font类的setBold(Z)V方法,从源码中进入Font类中,发现的确没有setBold(Z)V这个方法,但是不科学啊,EasyExcel底层是封装了POI的,那么它里面一定引用了POI的jar包,相当于调用自己内部代码的方法,不可能会找不到方法啊

  • 突然意识到
    记一次解决jar包版本冲突_第2张图片
    既然EasyExcel内部引用了POI的jar包,但是我本来的pom.xml文件中也引用POI的jar,会不会是jar包版本冲突造成的呢?接下来,分别查看了本身pom.xml引用POI的版本,为3.8,EasyExcel版本1.1.2-beat1内部引用的POI版本为3.17

  • 确认猜测

那么就到POI版本为3.8和3.17当中找到org.apache.poi.ss.usermodel.Font类看是否有setBold(Z)V这个方法
记一次解决jar包版本冲突_第3张图片
记一次解决jar包版本冲突_第4张图片
发现在POI3.8版本中Font这个类中并没有setBold(Z)V这个方法,但是在POI3.17版本中Font这个类中却有setBold(Z)V这个方法

  • 确认原因

至此,差不多可以知道到底是什么原因造成控制台抛出这异常了:原因是EasyExcel 1.1.2-beat1版本中本应使用POI 3.17版本中的代码,但是由于版本冲突却使用了POI 3.8版本中的代码,造成类中方法找不到而抛出这个异常的

  • 那么为什么会造成版本冲突呢?

maven在做版本管理时有一个重要的原则就是就近原则。啥是就是就近原则呢?就拿本例子来说,POI 3.8版本距离当前jar更近,而POI3.17版本更远(POI 3.17版本还经过了一层EasyExcel 1.1.2-beat1版本),所以在maven版本管理中,将使用POI 3.8版本

  • 那么如何解决呢?

将自身pom.xml文件中的POI 3.8依赖注释掉即可

  • 注释了再ReImport

发现还是报这个错

  • 还存在的问题

突然想到会不会是依赖其他模块,在其他模块中也依赖了POI 3.8版本,导致在依赖的过程中也将依赖中的POI 3.8版本的jar也依赖进来了,IDEA使用Ctrl+Shirt+F全项目搜索poi,发现果然在依赖的模块中也存在。

  • 那么如何解决呢?

在模块的依赖中除去POI的jar包


	com.wyj
	test
	0.0.1.RELEASE

换成


	com.wyj
	test
	0.0.1.RELEASE
	
		
			org.apache.poi
			poi
		
		
			org.apache.poi
			poi-ooxml
		
		
			org.apache.poi
			poi-ooxml-schemas
		
	

  • 完成之后再ReImport

运行完美解决

你可能感兴趣的:(Maven,Bug)