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

执行mvn clean install时Maven构建失败,疑与JDK版本相关

问题描述

原本能正常运行的Spring Boot Eureka服务,执行mvn clean install后构建失败,报错信息如下:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.521 s
[INFO] Finished at: 2023-11-28T23:15:57-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project eureka: Fatal error compiling: invalid target release: 11 -> [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/MojoExecutionException

已尝试以下操作但问题未解决:

  • 修改pom.xml中的java.version为11
  • 设置JAVA_HOME指向JDK11
  • 将IntelliJ IDE的JDK从openJDK-20切换为11

当前服务无法启动,使用的Maven版本为3.9.5,项目pom.xml内容如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.17</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rockysingh</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot Eureka server</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.8</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
解决方案
  • 确认Maven实际使用的JDK版本
    执行命令mvn -v,查看输出里的Java版本,确认是不是JDK11。如果不是,说明Maven没用到正确的JDK,要么调整系统环境变量让JDK11优先级更高,要么在IDE里单独给Maven配置JDK11。

  • 强制指定Maven编译插件的JDK参数
    在pom.xml的<build><plugins>节点里添加maven-compiler-plugin的配置,明确指定source和target版本为11,避免继承配置冲突:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
            <source>11</source>
            <target>11</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
    
  • 清理IDE缓存并重新加载项目
    在IntelliJ IDE中,点击File -> Invalidate Caches...,选择清理缓存并重启IDE,之后重新导入Maven项目,确保IDE配置和pom.xml同步。

  • 检查系统PATH中的JDK顺序
    确保系统PATH环境变量里,JDK11的bin目录排在其他JDK版本的前面,这样系统调用java、javac命令时会优先用JDK11。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 03:39:56