Spring 5.1 + Tomcat 9 + Java 10 + moduled project = java.lang.NoClassDefFoundError: org/springframework/core/io/Resource

Spring 5.1 + Tomcat 9 + Java 10 + moduled project = java.lang.NoClassDefFoundError: org/springframework/core/io/Resource

我完全被错误困住了:

SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
Caused by: java.lang.NoClassDefFoundError:     org/springframework/core/io/Resource

我正在尝试使用 Java 10、Spring 5.1 和 Tomcat 9 创建一个分为多个模块(如核心、身份验证等)的项目.

所有webapp配置都存储在app模块中。 这是 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     version="4.0">
<display-name>Prolly REST API</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/restapi-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>restapi</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>restapi</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

这是 restapi-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.roilen.code.prolly.core.rest"/>
<mvc:annotation-driven/>

这是主要的 pom.xml(基于父项和项目):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.roilen.code.prolly</groupId>
<artifactId>ProllyServer</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>core</module>
    <module>app</module>
</modules>
<packaging>pom</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <packaging.type>jar</packaging.type>
    <java.version>10</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <org.springframework.version>5.1.0.RELEASE</org.springframework.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
</dependencies>

这是一个应用程序模块 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>ProllyServer</artifactId>
    <groupId>com.roilen.code.prolly</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>app</artifactId>
<packaging>war</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
        </plugin>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.4.0.RC2</version>
            <configuration>
                <scanIntervalSeconds>3</scanIntervalSeconds>
                <webAppConfig>
                    <contextPath>/prolly</contextPath>
                    <extraClasspath>
                        target/classes;../core/target/classes;.asses;
                    </extraClasspath>
                </webAppConfig>
                <scanTargets>
                    <scanTarget>src/main/webapp</scanTarget>
                    <scanTarget>target/classes</scanTarget>
                    <scanTarget>../core/target/classes</scanTarget>
                </scanTargets>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**</include>
            </includes>
        </resource>
    </resources>
</build>

<dependencies>
    <dependency>
        <groupId>com.roilen.code.prolly</groupId>
        <artifactId>core</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

因此,当我尝试通过 Intellij IDEA 将项目部署到本地 tomcat 服务器时,出现了上述错误。 可以在此处找到该项目的来源:https://drive.google.com/open?id=1ThH9BCLpEYvrTwglkqrRhjIpv4nJ_YcW

谢谢!

好的,所以问题和往常一样简单。错误是项目设置里有两个war,部署到服务器的那个是错误的(项目核心war,不是app模块)

所以,我更改了配置设置中的 war,瞧!

希望这可以为那些创建模块化应用程序的人节省几个小时 :)