java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot项目编译

SpringBoot项目编译失败问题分析及解决方案

作者:程序员1970

本文总结了SpringBoot项目编译失败的主要类型及解决方法,包括JDK版本不匹配、依赖冲突、Maven配置错误、XML格式错误、配置文件编码问题、依赖注入错误、端口冲突等,通过详细的报错内容、原因分析和解决方案,帮助开发者定位和解决编译问题,感兴趣的朋友一起看看吧

常见编译失败类型及具体报错内容

1. JDK版本与SpringBoot版本不匹配

报错内容

Error: Class initialization failed: org.springframework.boot.SpringApplication

org/springframework/boot/maven/RepackageMojo has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0

原因

解决方案

<!-- Spring Boot 3.x 项目应使用 JDK 17+ -->
<properties>
    <java.version>17</java.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>
<!-- Spring Boot 2.x 项目可使用 JDK 8 -->
<properties>
    <java.version>8</java.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

2. 依赖冲突问题

(1) 依赖版本不一致

报错内容

java.lang.NoClassDefFoundError: org/springframework/boot/web/server/WebServerFactoryCustomizer

原因

解决方案

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.18</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

(2) 依赖传递冲突

报错内容

java.lang.NoSuchMethodError: com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.<init>(Lcom/baomidou/mybatisplus/core/parser/ISqlParser;)V

原因

解决方案

<dependency>
    <groupId>com.zjbdos.cloud</groupId>
    <artifactId>zjbdos-cloud-framework-core</artifactId>
    <version>1.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. Maven配置问题

(1) Maven编译插件配置错误

报错内容

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project app: Fatal error compiling: 无效的标记: --release

原因

解决方案

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <release>17</release>
            </configuration>
        </plugin>
    </plugins>
</build>

(2) 依赖坐标错误

报错内容

Could not find artifact com.zjbdos.cloud:zjbdos-cloud-framework-core:pom:1.0.0 in alimaven (http://maven.aliyun.com/nexus/content/groups/public/)

原因

解决方案

4. 资源文件与配置文件问题

(1) XML格式错误

报错内容

org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The file 'com/example/mapper/UserMapper.xml' is not valid.

原因

解决方案

<!-- XML文件中特殊字符转义 -->
<select id="selectUser" resultType="User">
    SELECT * FROM user WHERE name = #{name} AND status = 'ACTIVE' <!-- 注意单引号 -->
</select>

(2) 配置文件编码问题

报错内容

org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping

原因

解决方案

5. GraalVM原生编译问题

报错内容

Error: Class initialization failed: org.springframework.boot.SpringApplication

原因

解决方案

// 使用@RegisterForReflection
@RegisterForReflection
public class User {
    private String name;
    // getter/setter
}
// 或创建reflect-config.json
[
    {
        "name": "com.example.demo.User",
        "methods": [
            { "name": "<init>", "parameterTypes": [] },
            { "name": "getName", "parameterTypes": [] }
        ]
    }
]

6. 依赖注入错误

(1) 依赖注入失败

报错内容

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is java.lang.IllegalStateException: Cannot create a bean of type 'org.springframework.web.client.RestTemplate' because no matching factory method exists

原因

解决方案

// 方法一:升级Spring Boot版本
// 方法二:手动创建RestTemplate
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

7. 端口冲突问题

报错内容

[ERROR] 2023-11-08 19:19:09.123 [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
[ERROR] 2023-11-08 19:19:09.134 [main] o.s.b.web.embedded.tomcat.TomcatWebServer : Tomcat failed to start
[ERROR] 2023-11-08 19:19:09.135 [main] o.s.b.web.embedded.tomcat.TomcatWebServer : Context failed to start
[ERROR] 2023-11-08 19:19:09.136 [main] o.s.b.SpringApplication : Application run failed
java.net.BindException: Address already in use: bind

原因

解决方案

# application.properties
server.port=8081

三、编译失败问题排查流程

1. 查看详细错误日志

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project app: Fatal error compiling: 无效的标记: --release

关键点

2. 检查JDK版本

java -version

关键点

3. 分析依赖树

mvn dependency:tree

关键点

4. 检查依赖范围

mvn dependency:tree -Dincludes=org.springframework.boot

关键点

5. 验证配置文件

四、常见编译失败问题汇总表

问题类型报错内容原因解决方案
JDK版本不匹配Error: Class initialization failed: org.springframework.boot.SpringApplicationSpring Boot 3.x 需要 JDK 17+升级JDK或降低Spring Boot版本
依赖冲突NoClassDefFoundError: org/springframework/boot/web/server/WebServerFactoryCustomizer依赖版本不一致使用dependencyManagement统一版本
Maven配置错误Fatal error compiling: 无效的标记: --releaseMaven编译插件配置不正确配置正确的sourcetarget
依赖坐标错误Could not find artifact com.zjbdos.cloud:zjbdos-cloud-framework-core:pom:1.0.0依赖坐标错误确认依赖坐标和仓库配置
XML格式错误org.apache.ibatis.builder.BuilderException: Error parsing Mapper XMLXML格式错误修复XML格式,转义特殊字符
配置文件编码org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping配置文件编码不是UTF-8将文件编码转换为UTF-8
依赖注入失败NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.client.RestTemplate' availableSpring Boot版本不匹配升级Spring Boot或手动创建Bean
端口冲突java.net.BindException: Address already in use: bind端口被占用更改server.port或结束占用进程

五、最佳实践建议

1. 依赖管理规范

<properties>
    <java.version>17</java.version>
    <spring-boot.version>3.2.8</spring-boot.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. 依赖冲突排查流程

  1. 使用mvn dependency:tree分析依赖树
  2. 找出冲突的具体依赖
  3. 使用<exclusions>排除冲突依赖
  4. 重新打包并测试

3. 项目初始化最佳实践

4. 日志优化建议

# application.properties
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.com.example=DEBUG

六、归纳下

SpringBoot项目编译失败问题主要分为以下几类:

  1. 版本不匹配:JDK版本与Spring Boot版本不匹配
  2. 依赖冲突:依赖版本不一致导致类找不到、方法找不到
  3. 配置错误:Maven配置、XML格式、配置文件编码等问题
  4. 特殊场景:GraalVM原生编译、依赖注入等

核心排查原则

到此这篇关于SpringBoot项目编译失败问题分析及解决方案的文章就介绍到这了,更多相关SpringBoot项目编译内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文