【问题标题】:ClassNotFound With Maven WAR fileClassNotFound 与 Maven WAR 文件
【发布时间】:2020-05-01 21:43:10
【问题描述】:

我四处寻找解决方案,但可以找到任何合理的方法。它们中的大多数都与 JAR 有关。我不断收到这个令人沮丧的错误:

2020 年 1 月 15 日 11:36:06.605 严重 [Catalina-utility-1] org.apache.catalina.core.StandardContext.listenerStart 异常将上下文初始化事件发送到类 [org.springframework.web.context .ContextLoaderListener] java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:269)

它来自 commons-logging.jar (1.2)。

我已经添加了我的依赖,比如:

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
</dependency>

版本来自父pom.xml

我有这两个值得注意的插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <inherited>false</inherited>
    <executions>
        <execution>
            <id>pal-copy-dependencies</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
                <excludeScope>provided</excludeScope>
                <excludeClassifiers>shared</excludeClassifiers>
                <excludeTransitive>true</excludeTransitive>
                <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                <overWriteIfNewer>true</overWriteIfNewer>
                <silent>true</silent>
                <useBaseVersion>false</useBaseVersion>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <inherited>false</inherited>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <addDefaultEntries>true</addDefaultEntries>
                <addBuildEnvironmentEntries>true</addBuildEnvironmentEntries>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <classpathPrefix>WEB-INF/lib/</classpathPrefix>
            </manifest>
        </archive>
        <attachClasses>true</attachClasses>
        <classesClassifier>shared</classesClassifier>
        <failOnMissingWebXml>true</failOnMissingWebXml>
        <includeEmptyDirectories>true</includeEmptyDirectories>
        <outputDirectory>${project.basedir}\lib</outputDirectory>
        <webResources>
            <webResource>
                <directory>src/main/resources/ProcessOrderService</directory>
                <include>*.*</include>
                <targetPath>/ProcessOrderService/wsdl/</targetPath>
            </webResource>
        </webResources>
    </configuration>
</plugin>

我不知道我做错了什么。更重要的是,当我将 commons-logging.jar 放在 Tomcat 的 lib 文件夹中时,它就像一个魅力。

我在这个 war 文件中有一个依赖项:

<dependency>
    <groupId>com.test.mypackage</groupId>
    <artifactId>common-files</artifactId>
    <version>${project.version}</version>
    <type>jar</type>
    <scope>provided</scope>
</dependency>

这也使用公共日志记录,范围为compile。但是这种依赖必须保持provided。改变不了!

唯一可能导致冲突或其他问题的其他依赖项是 activemq-core.jar,但我添加了 excludes 参数。但是还是不行。

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 我刚刚注意到common-files 和战争之间常见的任何依赖关系都会导致问题。

标签: java maven war maven-dependency


【解决方案1】:

Tomcat 类加载器将应用程序本地依赖项视为最后一个 (https://tomcat.apache.org/tomcat-8.5-doc/class-loader-howto.html)。

我的最佳猜测是在 Tomcat 类路径的某个地方已经有这个库(不同版本)(毫不奇怪,因为它是一个 Apache 日志库)。

您可以尝试将 commons-logging 定义为可选,它不会被传递导入并且将使用 Tomcat 中可用的内容(假设该版本与您的代码配合良好)。或者,您需要找到库并替换/升级它。

【讨论】:

  • 所以你是说,在我的 WAR 和 common-files 依赖中都创建 &lt;optional&gt;true&lt;/optional&gt;
  • 在你的 POM 中声明 common-files 依赖时使用
  • 啊。谢谢。让我试试看。
  • 我会选择你的作为答案。它确实将我推向了正确的方向。虽然要让它工作,我需要弄清楚tomcat下的shared.loader东西。问题是如果它从provided 依赖中“继承”,那么一切都提供了。如果我稍后找到完整的解决方案,我稍后会写下来。谢谢您的帮助。那个加载器的东西很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多