java Maven 和 Tomcat 中的 Jar 冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5496809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:24:15  来源:igfitidea点击:

Jar conflict in Maven and Tomcat?

javatomcatjarmavenstruts2

提问by Hoàng Long

I'm having some trouble with jar loading. I deployed a Struts2 web application on Tomcat, and it resulted in error:

我在加载 jar 时遇到了一些问题。我在Tomcat上部署了一个Struts2 web应用,结果报错:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:....../Tomcat%206.0/lib/slf4j-log4j12-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:....../Tomcat%206.0/webapps/Timesheet/WEB-INF/lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
2011-03-31 14:33:48,302 DEBUG com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles:748 - Loading action configurations from: struts-default.xml
2011-03-31 14:33:50,592 DEBUG com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles:832 - Loaded action configuration from: struts-default.xml
...
2011-03-31 14:33:50,809 DEBUG com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register:202 - Loaded type:com.opensymphony.xwork2.util.XWorkConverter name:struts impl:com.opensymphony
.xwork2.util.AnnotationXWorkConverter
Mar 31, 2011 2:33:50 PM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
Mar 31, 2011 2:33:50 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/Timesheet] startup failed due to previous errors

I have excluded all jar-conflicts in pom.xml, but it seems to be another slf4j-log4jfile in Tomcat lib.

我已经排除了 .jar 中的所有 jar-conflicts pom.xml,但它似乎是slf4j-log4jTomcat lib 中的另一个文件。

Then I tried to remove the slf4j-log4j12-1.5.8.jar in Tomcat/lib and re-run the war again, but still got another error:

然后我尝试删除Tomcat/lib中的slf4j-log4j12-1.5.8.jar并再次重新运行war,但仍然出现另一个错误:

Mar 31, 2011 2:44:51 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(...\Tomcat 6.0\webapps\Timesheet\WEB-INF\lib\servlet-api-2.4.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class:
javax/servlet/Servlet.class
... // Struts file still loaded here, but another Error filterStart: the same as above.

Does I miss anything here?

我在这里想念什么吗?

EDIT:I have remove the redundant servlet-api included in pom.xml: it is accidentally included by another jar. But after excluding that jar I got the error:

编辑:我已经删除了 pom.xml 中包含的冗余 servlet-api:它被另一个 jar 意外包含。但是在排除那个 jar 之后,我得到了错误:

Mar 31, 2011 4:11:19 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(...\Tomcat 6.0\webapps\Timesheet\WEB-INF\lib\servlet-api-2.4.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class:
javax/servlet/Servlet.class
2011-03-31 16:11:20,234 DEBUG com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles:748 - Loading action configurations from: struts-default.xml
2011-03-31 16:11:21,028 DEBUG com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles:832 - Loaded action configuration from: struts-default.xml

My servlet-api in my Tomcat has version 2.5; the excluded servlet-api.jar in pom.xml has version 2.4.

我的 Tomcat 中的 servlet-api 版本为 2.5;pom.xml 中排除的 servlet-api.jar 版本为 2.4。

回答by Bozho

I don't think tomcat comes with slf4j bundled, so removing it from tomcat/libis a proper step.

我不认为 tomcat 捆绑了 slf4j,因此将其删除tomcat/lib是正确的步骤。

Then, you should not have servlet-api-x.jarin WEB-INF/lib, because it bundled with tomcat. Mark it as <scope>provided</scope>in the maven pom.

那么,你不应该有servlet-api-x.jarin WEB-INF/lib,因为它与 tomcat 捆绑在一起。将其标记为<scope>provided</scope>在 maven pom 中。

To make sure everything is cleaned-up call mvn clean

确保一切都被清理干净 mvn clean

回答by prunge

It seems that something is pulling in servlet-api-2.4.jar which is not supposed to be deployed as part of a webapp. If you have servlet-api as a dependency in your project, ensure it has a scope of provided. This tells maven to use it for compilation but not for packaging. (see this Maven FAQ entryand Introduction to the Dependency Mechanism).

似乎有些东西正在拉入 servlet-api-2.4.jar,它不应该作为 web 应用程序的一部分进行部署。如果您的项目中将 servlet-api 作为依赖项,请确保它的范围为provided。这告诉 maven 将它用于编译而不是用于打包。(请参阅此 Maven 常见问题条目依赖机制简介)。

However, servlet-api might also be being pulled in because it is referenced transitively from one of your project dependencies. If this is the case, trying running:

但是,servlet-api 也可能被引入,因为它是从您的项目依赖项之一传递引用的。如果是这种情况,请尝试运行:

mvndependency:tree

mvn依赖:树

to print out a list of transitive dependencies and where they are coming from. In the output, search for the servlet-api dependency with a non-provided scope. You can then remove the offender by adding an exclude to its ancestor dependency in your POM.

打印出传递依赖项的列表以及它们的来源。在输出中,使用未提供的范围搜索 servlet-api 依赖项。然后,您可以通过向 POM 中的祖先依赖项添加排除项来删除违规者。