如何解决web项目中遇到的Maven包依赖冲突问题
HTTP Status 500 - java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/compiler/IProblem;
type Exception report
message Java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/compiler/IProblem;
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/compiler/IProblem;
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:273)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/compiler/IProblem; org.apache.jasper.compiler.JDTCompiler$2.acceptResult(JDTCompiler.java:354) org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:480) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:425) org.apache.jasper.compiler.Compiler.compile(Compiler.java:298) org.apache.jasper.compiler.Compiler.compile(Compiler.java:277) org.apache.jasper.compiler.Compiler.compile(Compiler.java:265) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) javax.servlet.http.HttpServlet.service(HttpServlet.java:620) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
经过初步查看堆栈,大意是将JSP编译成Servlet产生了错误,类不匹配,怀疑可能是JSTL,Servlet和Tomcat版本产生了冲突。一番折腾,没有进展。
于是思考了一下,刚才只不过是添加了hadoop hdfs相关的jar包:
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>2.2.0</version> </dependency>
难道是其中包含了一些特殊的依赖?通过IntelliJ Idea中的pom编辑器,可以显示Maven Show Dependencies:
可以看出所有的依赖关系,果然,hadoop-common包依赖了servlet-api, jasper-runtime等可能与当前环境冲突的jar包,由于这些都是通过依赖关系链产生的关系,所以非常不容易被察觉。
同样,也可以通过mvn自带的工具在项目中命令行操作得到整个依赖的树形结构:
mvn dependency: tree [INFO] com.xxx:diablo-framework:war:1.0-SNAPSHOT [INFO] +- javax.servlet:jstl:jar:1.2:compile [INFO] +- javax:javaee-api:jar:6.0:provided [INFO] +- log4j:log4j:jar:1.2.8:compile [INFO] +- com.sun.mail:javax.mail:jar:1.5.2:compile [INFO] | \- javax.activation:activation:jar:1.1:compile [INFO] +- commons-io:commons-io:jar:2.4:compile [INFO] +- commons-beanutils:commons-beanutils:jar:1.9.2:compile [INFO] | +- commons-logging:commons-logging:jar:1.1.1:compile [INFO] | \- commons-collections:commons-collections:jar:3.2.1:compile [INFO] +- commons-lang:commons-lang:jar:2.6:compile [INFO] +- org.codehaus.jackson:jackson-xc:jar:1.9.13:compile [INFO] | \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile [INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile [INFO] +- commons-httpclient:commons-httpclient:jar:3.1:test [INFO] | \- commons-codec:commons-codec:jar:1.2:compile [INFO] +- junit:junit:jar:4.11:test [INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] +- org.springframework:spring-core:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:3.2.2.RELEASE:compile [INFO] | \- aopalliance:aopalliance:jar:1.0:compile [INFO] +- org.springframework:spring-aspects:jar:3.2.2.RELEASE:compile [INFO] | \- org.aspectj:aspectjweaver:jar:1.7.2:compile [INFO] +- org.springframework:spring-beans:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-context-support:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-dao:jar:2.0.8:compile [INFO] +- org.springframework:spring-expression:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-jdbc:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-jms:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-orm:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-oxm:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-test:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-tx:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-web:jar:3.2.2.RELEASE:compile [INFO] +- org.springframework:spring-webmvc:jar:3.2.2.RELEASE:compile [INFO] +- org.apache.commons:commons-exec:jar:1.3:compile [INFO] +- commons-dbcp:commons-dbcp:jar:1.4:compile [INFO] | \- commons-pool:commons-pool:jar:1.5.4:compile [INFO] +- org.hibernate:hibernate-core:jar:3.6.10.Final:compile [INFO] | +- antlr:antlr:jar:2.7.6:compile [INFO] | +- dom4j:dom4j:jar:1.6.1:compile [INFO] | +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile [INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile [INFO] | +- javax.transaction:jta:jar:1.1:compile [INFO] | \- org.slf4j:slf4j-api:jar:1.6.1:compile [INFO] +- org.hibernate:hibernate-entitymanager:jar:3.6.10.Final:compile [INFO] | +- cglib:cglib:jar:2.2:compile [INFO] | | \- asm:asm:jar:3.1:compile [INFO] | \- javassist:javassist:jar:3.12.0.GA:compile [INFO] +- net.sf.ehcache:ehcache-core:jar:2.6.9:compile [INFO] +- mysql:mysql-connector-java:jar:5.1.31:compile [INFO] +- org.hsqldb:hsqldb:jar:2.2.9:compile [INFO] +- commons-fileupload:commons-fileupload:jar:1.3.1:compile [INFO] +- org.apache.poi:poi:jar:3.5-FINAL:compile [INFO] +- org.apache.poi:poi-ooxml:jar:3.5-FINAL:compile [INFO] | \- org.apache.poi:ooxml-schemas:jar:1.0:compile [INFO] | \- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile [INFO] | \- stax:stax-api:jar:1.0.1:compile [INFO] +- org.apache.oozie:oozie-client:jar:4.1.0:compile [INFO] | +- com.googlecode.json-simple:json-simple:jar:1.1:compile [INFO] | +- commons-cli:commons-cli:jar:1.2:compile [INFO] | +- com.google.guava:guava:jar:11.0.2:compile [INFO] | | \- com.google.code.findbugs:jsr305:jar:1.3.9:compile [INFO] | +- org.apache.activemq:activemq-client:jar:5.8.0:compile [INFO] | | +- org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:compile [INFO] | | +- org.fusesource.hawtbuf:hawtbuf:jar:1.9:compile [INFO] | | \- org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:jar:1.0.1:compile [INFO] | +- org.slf4j:slf4j-simple:jar:1.6.6:compile [INFO] | \- xerces:xercesImpl:jar:2.10.0:compile [INFO] | \- xml-apis:xml-apis:jar:1.4.01:compile [INFO] +- org.freemarker:freemarker:jar:2.3.20:compile [INFO] +- org.apache.hadoop:hadoop-common:jar:2.2.0:compile [INFO] | +- org.apache.hadoop:hadoop-annotations:jar:2.2.0:compile [INFO] | +- org.apache.commons:commons-math:jar:2.1:compile [INFO] | +- xmlenc:xmlenc:jar:0.52:compile [INFO] | +- commons-net:commons-net:jar:3.1:compile [INFO] | +- org.mortbay.jetty:jetty:jar:6.1.26:compile [INFO] | +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile [INFO] | +- com.sun.jersey:jersey-core:jar:1.9:compile [INFO] | +- com.sun.jersey:jersey-json:jar:1.9:compile [INFO] | | +- org.codehaus.jettison:jettison:jar:1.1:compile [INFO] | | +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile [INFO] | | | \- javax.xml.bind:jaxb-api:jar:2.2.2:compile [INFO] | | \- org.codehaus.jackson:jackson-jaxrs:jar:1.8.3:compile [INFO] | +- com.sun.jersey:jersey-server:jar:1.9:compile [INFO] | +- commons-el:commons-el:jar:1.0:compile [INFO] | +- net.java.dev.jets3t:jets3t:jar:0.6.1:compile [INFO] | +- commons-configuration:commons-configuration:jar:1.6:compile [INFO] | | +- commons-digester:commons-digester:jar:1.8:compile [INFO] | | \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile [INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile [INFO] | +- org.apache.avro:avro:jar:1.7.4:compile [INFO] | | +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile [INFO] | | \- org.xerial.snappy:snappy-java:jar:1.0.4.1:compile [INFO] | +- com.google.protobuf:protobuf-java:jar:2.5.0:compile [INFO] | +- com.jcraft:jsch:jar:0.1.42:compile [INFO] | +- org.apache.zookeeper:zookeeper:jar:3.4.5:compile [INFO] | \- org.apache.commons:commons-compress:jar:1.4.1:compile [INFO] | \- org.tukaani:xz:jar:1.0:compile [INFO] +- org.apache.hadoop:hadoop-hdfs:jar:2.2.0:compile [INFO] | +- commons-daemon:commons-daemon:jar:1.0.13:compile [INFO] | +- javax.servlet.jsp:jsp-api:jar:2.1:compile [INFO] | \- tomcat:jasper-runtime:jar:5.5.23:compile [INFO] +- org.apache.hadoop:hadoop-auth:jar:2.2.0:compile [INFO] +- com.xxx:diablo.communication:jar:1.0-SNAPSHOT:compile [INFO] +- com.caucho:hessian:jar:3.1.5:compile [INFO] \- org.quartz-scheduler:quartz:jar:1.8.4:compile
于是,通过右键将这些jar包排除(Exclude)出去,最后编辑生成的依赖成为了这样:
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.2.0</version> <exclusions> <exclusion> <artifactId>servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion> <exclusion> <artifactId>jasper-compiler</artifactId> <groupId>tomcat</groupId> </exclusion> <exclusion> <artifactId>jasper-runtime</artifactId> <groupId>tomcat</groupId> </exclusion> <exclusion> <artifactId>jsp-api</artifactId> <groupId>javax.servlet.jsp</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.2.0</version> <exclusions> <exclusion> <artifactId>servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion> </exclusions> </dependency>
问题才得以解决,在普通的Java应用中,当出现相同版本的jar包,class时,是根据启动时的CLASSPATH先后顺序来决定最终使用的是哪个类型,但是这样就非常不确定。虽然OSGi技术可以通过隔离ClassLoader的方式解决这个问题,但OSGi本身稍显复杂臃肿,学习成本比较高,而且同样需要一个OSGi容器。这就要求我们在平时引入新的jar包时能够对其进行依赖分析,找出可能产生冲突的jar包,最终解决问题。