You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目maven-compiler-plugin找不到模块A类问题求助

排查方向与解决方案

1. 模块A的Spring Boot打包方式导致依赖无法识别

如果模块A是Spring Boot项目,默认使用spring-boot-maven-plugin打包生成的是可执行Jar,其类文件存放在BOOT-INF/classes目录下,而非标准Jar的根目录。这种Jar无法被其他模块作为依赖正常引用,Maven编译模块B时会找不到类。

解决办法:

  • 在模块A的pom.xml中配置spring-boot-maven-plugin,让其同时生成可执行Jar和普通Jar(供其他模块依赖):
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <!-- 生成普通Jar,classifier为exec的是可执行Jar -->
        <classifier>exec</classifier>
    </configuration>
</plugin>
  • 模块B依赖模块A时,直接引用不带classifier的普通Jar即可(Maven默认会匹配普通Jar)。

2. 模块B中模块A的依赖Scope配置错误

如果模块A的依赖Scope被设为runtime,Maven编译阶段不会引入该依赖的类文件,仅在运行时加载,这会导致编译模块B时找不到类(IDE会自动补全依赖,所以单独运行测试正常)。

解决办法:

  • 检查模块B的pom.xml中模块A的依赖配置,确保Scope为默认的compile(可省略不写):
<dependency>
    <groupId>你的groupId</groupId>
    <artifactId>模块A的artifactId</artifactId>
    <version>版本号</version>
    <!-- 不要设置为runtime或provided -->
</dependency>

3. Maven构建顺序未关联模块A与模块B

如果模块A和模块B是独立的Maven模块(未在同一个父项目中),直接在模块B目录执行mvn clean install时,Maven不会自动先构建并安装模块A到本地仓库。若本地仓库中模块A的Jar已被清理,就会出现编译错误。

解决办法:

  • 将两个模块加入同一个Maven父项目,在父项目的pom.xml中声明子模块:
<modules>
    <module>../模块A</module>
    <module>../模块B</module>
</modules>
  • 之后在父项目目录执行mvn clean install,Maven会按正确顺序先构建模块A,再构建模块B。
  • 若不想用父项目,每次构建模块B前先在模块A目录执行mvn clean install,确保模块A的Jar存在于本地仓库。

4. 模块A的核心类被放在Test目录下

如果模块A中被模块B引用的类是写在src/test/java目录下,这些类不会被打包到模块A的主Jar中,模块B依赖主Jar自然找不到类(IDE运行测试时会加载Test目录的类,所以单独测试正常)。

解决办法:

  • 将模块A中被模块B依赖的类移到src/main/java目录下。
  • 若必须保留在Test目录,模块B需要依赖模块A的Test Jar:
    模块B的pom.xml添加:
<dependency>
    <groupId>你的groupId</groupId>
    <artifactId>模块A的artifactId</artifactId>
    <version>版本号</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

模块A的pom.xml配置打包Test类:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

5. Maven Compiler插件配置不一致

若模块B的maven-compiler-plugin配置的JDK版本与模块A不一致,或者配置了不合理的排除规则,可能导致编译时无法识别模块A的类。

解决办法:

  • 统一模块A和模块B的JDK编译版本,在各自的pom.xml中配置:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>1.8</source> <!-- 根据项目实际JDK版本调整 -->
        <target>1.8</target>
    </configuration>
</plugin>
  • 检查模块B的Compiler插件是否有排除模块A类的配置,移除相关排除规则。

内容的提问来源于stack exchange,提问作者maihexi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 01:17:40