IDEA:maven-compiler-plugin: Fatal error compiling: 无效的目标发行版: 1.9

博主记录一次报错经历,起初以为是JDK编译与运行版本不匹配,将Idea里的1.9改成与本地安装一致的1.8仍报错。苦思后发现是settings.xml文件里将JDK配置为1.9,导致Maven插件报错,改成1.8后问题解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一次error

本以为是 jdk编译版本和运行版本不匹配,idea里面是 1.9 ,本地安装是1.8 的 ,修改成一致的1.8还是 会报错;

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Fatal error compiling: 无效的目标发行版: 1.9 -> [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

在这里插入图片描述
苦思了一晚上, 想起来 可能是mvn配置的问题, 好久没看mvn的相关知识了;
如下图,在settings.xml文件里配置了 ==jdk为1.9 ==所以才会报 这个 maven 插件的错误;
在这里插入图片描述

改成 1.8就好了

	<profile>
	  <id>jdk-1.8</id>
	  <activation>
	    <activeByDefault>true</activeByDefault>
	    <jdk>1.8</jdk>
	  </activation>
	  <properties>
	    <maven.compiler.source>1.8</maven.compiler.source>
	    <maven.compiler.target>1.8</maven.compiler.target>
	    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	  </properties>
	</profile>
### Maven 编译时出现 &#39;无效的目标发行版: 17&#39; 错误的解决方案 在使用 Maven 构建项目时,如果遇到 `&#39;无效的目标发行版: 17&#39;` 的错误,通常是因为项目的 JDK 版本与 Maven 配置不匹配。以下是解决此问题的详细方法[^1]。 #### 1. 检查本地 JDK 版本 确保当前系统中安装的 JDK 版本支持 Java 17。可以通过以下命令检查: ```bash java -version ``` 如果输出显示的版本低于 17,则需要升级 JDK 到支持 Java 17 的版本[^2]。 #### 2. 更新 `maven-compiler-plugin` 配置 在 `pom.xml` 文件中,明确指定编译器插件的源代码和目标代码版本为 17。例如: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> ``` 上述配置确保了 Maven 使用 Java 17 进行编译[^3]。 #### 3. 设置 Maven 的 JDK 环境变量 如果系统的默认 JDK 版本低于 17,可以通过设置 Maven 的 `JAVA_HOME` 环境变量来指定正确的 JDK 路径。例如,在 Linux 或 macOS 中: ```bash export JAVA_HOME=/path/to/jdk-17 export PATH=$JAVA_HOME/bin:$PATH ``` 在 Windows 中,可以通过系统属性或命令行设置环境变量[^4]。 #### 4. 检查 IDE 配置(如适用) 如果使用 IDE(如 IntelliJ IDEA 或 Eclipse),需要确保项目的 JDK 和语言级别设置为 17。例如,在 IntelliJ IDEA 中: - 打开 **File > Project Structure**。 - 在 **Project** 选项卡中,将 **Project SDK** 设置为 JDK 17。 - 在 **Modules** 中,将 **Language level** 设置为 17[^5]。 #### 5. 清理并重新构建项目 完成上述配置后,运行以下命令清理并重新构建项目: ```bash mvn clean install ``` ### 示例代码片段 以下是一个完整的 `pom.xml` 配置示例,适用于 Spring Boot 项目: ```xml <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> </parent> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值