如何解决IDEA Build出错(附代码)

240 阅读1分钟

在IDEA 2021.1中构建项目时,出现以下错误:

org.springframework.web.servlet.config.annotation包不存在。

可怕的是,在命令行中运行mvn compile ,效果很好,但在IDEA中运行Build,就会出现这个错误。

之前的构建过程是正常的,但是最近我修改了我的一个依赖项JHCommonLibrary的源代码,这个依赖项是依赖于spring-webmvc的:

<dependency>
        <groupId>com.juhe</groupId>
        <artifactId>JHCommonLibrary</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

我从JHCommonLibrary中删除了spring-webmvc的依赖关系,但它没有工作:

<dependency>
        <groupId>com.juhe</groupId>
        <artifactId>JHCommonLibrary</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

然后我又明确地添加了spring-webmvc,但它还是不能工作:

<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.3.9.RELEASE</version>
      </dependency>

解决方案

最后,我把JHCommonLibrary移到<依赖关系>的末尾,现在可以工作了。或者在JHCommonLibrary之前加入spring-webmvc的依赖,也可以工作。

(我需要再次声明,这个问题只出现在IDEA中,如果在命令行中运行mvn compile ,则不会出现。所以这很可能是IDEA的一个bug)