java - 如何修复模块未找到 : spring. web 和类似错误

标签 java spring maven module-path

我正在尝试使用 Java 11 构建一个简单的 maven spring-rest 应用程序,但我不断收到“找不到模块错误”。

我在SO上看了很多类似的问题。我尝试了各种建议 - 删除 Maven 存储库、解决一些警告等,但我仍然无法使项目正常运行。

我的 parent pom:

<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.luv2code</groupId>
   <artifactId>spring-rest-demo</artifactId>
   <version>1.0</version>
    <modules>
        <module>nedim</module>
    </modules>
    <packaging>pom</packaging>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>11</maven.compiler.source>
      <maven.compiler.target>11</maven.compiler.target>
   </properties>

   <dependencies>

      <!-- Add Spring MVC and REST support -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>5.0.5.RELEASE</version>
      </dependency>

      <!-- Add Jackson for JSON converters -->
      <dependency>
         <groupId>com.fasterxml.jackson.core</groupId>
         <artifactId>jackson-databind</artifactId>
         <version>2.9.5</version>
      </dependency>

      <!-- Add Servlet support for 
          Spring's AbstractAnnotationConfigDispatcherServletInitializer -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.1.0</version>
      </dependency>
   </dependencies>

   <!-- Support for Maven WAR Plugin -->

   <build>

      <finalName>spring-rest-demo</finalName>

      <pluginManagement>
         <plugins><plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>               
            </plugin>                 
         </plugins>
      </pluginManagement>
   </build>

</project>

子 pom:

<?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>spring-rest-demo</artifactId>
        <groupId>com.luv2code</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nedim</artifactId>


</project>

模块信息.java:

open module nedim {
    requires spring.context;
    requires spring.webmvc;
    requires spring.web; 
}

当我运行 maven clean install 时,我得到以下信息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project nedim: Compilation failure: Compilation failure: 
[ERROR] /D:/Workspace/Java/spring-rest-demo/nedim/src/main/java/module-info.java:[2,24] module not found: spring.context
[ERROR] /D:/Workspace/Java/spring-rest-demo/nedim/src/main/java/module-info.java:[3,24] module not found: spring.webmvc
[ERROR] /D:/Workspace/Java/spring-rest-demo/nedim/src/main/java/module-info.java:[4,24] module not found: spring.web
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :nedim

此外,我在 VM 选项中添加了 --show-module-resolution 并且列表中没有 spring 模块

最佳答案

主要问题是:org.apache.maven.plugins:maven-compiler-plugin:3.1:compile。这是一个非常旧的版本,不支持 JPMS。请更新到3.8.1

关于java - 如何修复模块未找到 : spring. web 和类似错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592690/

相关文章:

java - 如何创建最小的 spring-boot 可部署 .war 文件?

java - war中包含的运行时依赖

java - 将电话号码编码为单词并搜索字典

java.lang.NoSuchMethodError : org. eclipse.jetty.server.Request.getUri()Lorg/eclipse/jetty/http/HttpURI 问题

java - ConfigurableEmbeddedServletContainerFactory 的 Maven 依赖项在哪里?

java - 如何将Java字符串转换为Enum ConverterFactory类并转换为Kotlin

java - 将数据从 List 传输到二维数组

java - 如何检查某个方法是否被调用?

spring - JWT 与 Spring OAuth2

java - spring 通用应用程序事件无法到达目的地