如何修复“module not found: spring.web”及类似错误

4
我将尝试使用Java 11构建一个简单的maven spring-rest应用,但是我不断遇到“模块未找到错误”的问题。
我在SO上阅读了很多类似的问题。我尝试了各种建议 - 删除maven仓库、解决一些警告等,但我仍然无法让项目正常工作。
我的父级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>

module-info.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模块。

1个回答

6

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


谢谢,Robert!实际上,半个小时前我在这里找到了您对一个稍微不同的问题的答案:https://dev59.com/TKbja4cB1Zd3GeqPdT-6,它解决了我的问题。但现在我又有了另一个问题:似乎我的Spring配置和控制器中的映射(都在“nedim”模块中 - 模块名称仅用于测试)在项目初始化期间没有被使用。我错过了什么吗?难道spring-container现在不应该被初始化吗? - Nedim
抱歉...我刚才注意到日志:Artifact unnamed: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: D:\Workspace\Java\spring-rest-demo\out\artifacts\unnamed\unnamed.war未找到Web模块。我不确定这是什么意思,但显然这是个问题。 - Nedim
解决方案在这里:https://dev59.com/_FgQ5IYBdhLWcg3wJgeO#43251205。我将此记录作为可能参考,供有同样问题的人使用。 - Nedim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接