我对spring webapp maven构建有一个有趣的问题。当在eclipse中构建时,一切都很好但是当通过maven构建并部署到tomcat 8容器时,webapp在启动时失败,并出现以下错误:
Caused by: java.lang.NoClassDefFoundError: FilterConfig
现在我尝试了所有明显且记录良好的依赖性问题(在javax.servlet-api和jsp-api import上提供了设置范围,并确保它们是最新版本,因为这是一个Java 8项目)。我确保所有的编译器插件都是最新版本:
行家-战争插件 3.1.0
行家编译-插件 3.6.1
然而,webapp无法启动,maven构建控制台输出中没有显示任何错误。在(工作)eclipse构建和神秘失败的maven等价物之间进行了许多令人头疼和艰苦的比较之后,发现maven生成的一些类文件内部是描述编译问题的文本。 (以下内容摘自"打开>文本编辑器" eclipse中针对.class文件的选项 - 某些字符必须省略,因为它们无法正确复制)
Unresolved compilation problems:
The import javax.servlet.Filter cannot be resolved
The import javax.servlet.FilterChain cannot be resolved
The import javax.servlet.FilterConfig cannot be resolved
The import javax.servlet.ServletException cannot be resolved
The import javax.servlet.ServletRequest cannot be resolved
The import javax.servlet.ServletResponse cannot be resolved
The import javax.servlet.http.HttpServletRequest cannot be resolved
The import javax.servlet.http.HttpSession cannot be resolved
Filter cannot be resolved to a type
FilterConfig cannot be resolved to a type
ServletException cannot be resolved to a type
ServletRequest cannot be resolved to a type
ServletResponse cannot be resolved to a type
FilterChain cannot be resolved to a type
ServletException cannot be resolved to a type
HttpServletRequest cannot be resolved to a type
HttpServletRequest cannot be resolved to a type
HttpSession cannot be resolved to a type
我无法理解maven进程是如何成功完成的,并且我尝试过的任何mvn目标都没有显示任何错误
mvn编译器:编译
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxxx 2.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ EBPP ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ EBPP ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.413 s
[INFO] Finished at: 2017-06-28T15:11:45+01:00
[INFO] Final Memory: 9M/213M
[INFO] ------------------------------------------------------------------------
以上但是-x显示了很多调试输出,但没有任何建议编译问题,实际上它表明包含已识别的未解析依赖项的类文件
...
[DEBUG] javax.servlet:javax.servlet-api:jar:3.1.0:provided
[DEBUG] org.apache.velocity:velocity:jar:1.7:compile
[DEBUG] commons-collections:commons-collections:jar:3.2.1:compile
[DEBUG] commons-lang:commons-lang:jar:2.4:compile
[DEBUG] javax.servlet.jsp:jsp-api:jar:2.2:provided
...
mvn依赖:build-classpath
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxxx 2.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:build-classpath (default-cli) @ EBPP ---
[INFO] Dependencies classpath:
C:\Users\xxxxxx\.m2\repository\org\springframework\webflow\spring-webflow\2.4.5.RELEASE\spring-webflow-2.4.5.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\xxxxxx\.m2\repository\opensymphony\ognl\2.6.11\ognl-2.6.11.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\webflow\spring-binding\2.4.5.RELEASE\spring-binding-2.4.5.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\webflow\spring-js\2.4.5.RELEASE\spring-js-2.4.5.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\webflow\spring-js-resources\2.4.5.RELEASE\spring-js-resources-2.4.5.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-beans\4.3.0.RELEASE\spring-beans-4.3.0.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-context\4.3.0.RELEASE\spring-context-4.3.0.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-aop\4.3.0.RELEASE\spring-aop-4.3.0.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-expression\4.3.0.RELEASE\spring-expression-4.3.0.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-web\4.3.0.RELEASE\spring-web-4.3.0.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-webmvc\4.3.0.RELEASE\spring-webmvc-4.3.0.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-core\4.3.9.RELEASE\spring-core-4.3.9.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\com\xxxxxxx\ebpp\JComms\2.0.1\JComms-2.0.1.jar;C:\Users\xxxxxx\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\xxxxxx\.m2\repository\javax\mail\mail\1.4.7\mail-1.4.7.jar;C:\Users\xxxxxx\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\xxxxxx\.m2\repository\org\apache\httpcomponents\httpclient\4.5.1\httpclient-4.5.1.jar;C:\Users\xxxxxx\.m2\repository\org\apache\httpcomponents\httpcore\4.4.3\httpcore-4.4.3.jar;C:\Users\xxxxxx\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Users\xxxxxx\.m2\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;C:\Users\xxxxxx\.m2\repository\com\xxxxxxx\ebpp\JCore\2.0.1\JCore-2.0.1.jar;C:\Users\xxxxxx\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;C:\Users\xxxxxx\.m2\repository\org\apache\velocity\velocity\1.7\velocity-1.7.jar;C:\Users\xxxxxx\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Users\xxxxxx\.m2\repository\commons-lang\commons-lang\2.4\commons-lang-2.4.jar;C:\Users\xxxxxx\.m2\repository\javax\servlet\jsp\jsp-api\2.2\jsp-api-2.2.jar;C:\Users\xxxxxx\.m2\repository\junit\junit\3.8.2\junit-3.8.2.jar;C:\Users\xxxxxx\.m2\repository\org\springframework\spring-context-support\4.3.9.RELEASE\spring-context-support-4.3.9.RELEASE.jar;C:\Users\xxxxxx\.m2\repository\com\xxxxxxx\xxxxxxx\440\xxxxxxx-440.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.331 s
[INFO] Finished at: 2017-06-28T15:13:05+01:00
[INFO] Final Memory: 13M/213M
[INFO] ------------------------------------------------------------------------
mvn验证
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxx 2.0.1
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.109 s
[INFO] Finished at: 2017-06-28T15:15:32+01:00
[INFO] Final Memory: 7M/213M
[INFO] ------------------------------------------------------------------------
我真的很难过。我假设maven正在使用与eclipse不同的编译器,但我确实已经没有想法了。
之前有没有其他人遇到过这个?
提前致谢。
答案 0 :(得分:1)
我怀疑它是eclipse构建文件与编译错误的组合(是的,Eclipse可以做到这一点:如果你从未达到不可编译的代码,那么你甚至不会得到错误)并且Maven没有清理输出Eclipse首先。
然后Maven看到该文件是最新的,并且不会覆盖它。
我建议如下(检查我是否正确):
package
,但也运行clean
(即mvn clean package
);关键是要100%确定Maven构建那些文件而不是Eclipse。至于错误本身,看起来你确实错过了一个依赖项(但可能在Eclipse的类路径中,而不是Maven)。
答案 1 :(得分:0)
因此,当将此项目与另一个成功的项目(都是从Java 4转换)进行比较时,使用非标准目录名称的预感是mavens不稳定行为的原因(复制warSourceDirectory时)。我分析了(现在称为)WebRoot> WEB-INF> classes目录并发现,对于有问题的应用程序,这已被填充,您已经猜到了 - 错误编译的类文件!很明显,war插件将这些坏文件复制到Maven所拥有的文件顶部,在同一个构建过程中成功编译 - 因此没有错误。
我不确定为什么坏文件存在,因为它们不在旧的Java 4项目中。我怀疑它们必须是在使用不正确的设置的先前失败的maven构建期间以某种方式创建的。