当前位置: 首页 > news >正文

IDEA Maven构建时报错:无效的目标发行版17

报错分析

报错原因:Maven 构建时,Java 版本配置不匹配

我安装的JDK版本是1.8,但由于种种原因,Maven构建时指定了 Java 17 作为目标发行版,从而导致错误

解决方案

首先,java -version,查看环境变量是否设置为了想要的JDK版本

1.检查pom文件

由于是在Maven构建时报错,所以优先检查pom文件,查看是否在properties中设置了错误的compiler.sourcecompiler.target

如上所示则是正确的(因为使用的JDK版本是1.8)

两个参数的含义分别是:

maven.compiler.source设置为 1.8,表示使用 Java 8 的语法规则来编译源代码

maven.compiler.target设置为 1.8,表示生成的字节码版本为 Java 8

2.查看Maven的有效配置

在编译时,可以使用 maven-compiler-plugin 插件指定所使用的JDK的版本,如果没有指定的话,将会使用默认配置,可以通过mvn help:effective-pom指令来查看默认配置,打印输出的内容结构如下:

  <modelVersion>4.0.0</modelVersion><groupId>com.why</groupId><artifactId>Sort-Array-Desc</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.compilerVersion>17</maven.compiler.compilerVersion><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec</artifactId><version>3.1.3</version><scope>compile</scope></dependency></dependencies><repositories><repository><snapshots><enabled>false</enabled></snapshots><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url></repository></repositories><pluginRepositories><pluginRepository><releases><updatePolicy>never</updatePolicy></releases><snapshots><enabled>false</enabled></snapshots><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url></pluginRepository></pluginRepositories><build><sourceDirectory>D:\codes\java\hive\Sort-Array-Desc\src\main\java</sourceDirectory><scriptSourceDirectory>D:\codes\java\hive\Sort-Array-Desc\src\main\scripts</scriptSourceDirectory><testSourceDirectory>D:\codes\java\hive\Sort-Array-Desc\src\test\java</testSourceDirectory><outputDirectory>D:\codes\java\hive\Sort-Array-Desc\target\classes</outputDirectory><testOutputDirectory>D:\codes\java\hive\Sort-Array-Desc\target\test-classes</testOutputDirectory><resources><resource><directory>D:\codes\java\hive\Sort-Array-Desc\src\main\resources</directory></resource></resources><testResources><testResource><directory>D:\codes\java\hive\Sort-Array-Desc\src\test\resources</directory></testResource></testResources><directory>D:\codes\java\hive\Sort-Array-Desc\target</directory><finalName>Sort-Array-Desc</finalName><pluginManagement><plugins><plugin><artifactId>maven-antrun-plugin</artifactId><version>1.3</version></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><version>2.2-beta-5</version></plugin><plugin><artifactId>maven-dependency-plugin</artifactId><version>2.8</version></plugin><plugin><artifactId>maven-release-plugin</artifactId><version>2.5.3</version></plugin></plugins></pluginManagement><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><executions><execution><id>default-compile</id><phase>compile</phase><goals><goal>compile</goal></goals><configuration><source>1.8</source><target>1.8</target></configuration></execution><execution><id>default-testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals><configuration><source>1.8</source><target>1.8</target></configuration></execution></executions><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><artifactId>maven-clean-plugin</artifactId><version>2.5</version><executions><execution><id>default-clean</id><phase>clean</phase><goals><goal>clean</goal></goals></execution></executions></plugin><plugin><artifactId>maven-resources-plugin</artifactId><version>2.6</version><executions><execution><id>default-testResources</id><phase>process-test-resources</phase><goals><goal>testResources</goal></goals></execution><execution><id>default-resources</id><phase>process-resources</phase><goals><goal>resources</goal></goals></execution></executions></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>2.4</version><executions><execution><id>default-jar</id><phase>package</phase><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.12.4</version><executions><execution><id>default-test</id><phase>test</phase><goals><goal>test</goal></goals></execution></executions></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.4</version><executions><execution><id>default-install</id><phase>install</phase><goals><goal>install</goal></goals></execution></executions></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.7</version><executions><execution><id>default-deploy</id><phase>deploy</phase><goals><goal>deploy</goal></goals></execution></executions></plugin><plugin><artifactId>maven-site-plugin</artifactId><version>3.3</version><executions><execution><id>default-site</id><phase>site</phase><goals><goal>site</goal></goals><configuration><outputDirectory>D:\codes\java\hive\Sort-Array-Desc\target\site</outputDirectory><reportPlugins><reportPlugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-project-info-reports-plugin</artifactId></reportPlugin></reportPlugins></configuration></execution><execution><id>default-deploy</id><phase>site-deploy</phase><goals><goal>deploy</goal></goals><configuration><outputDirectory>D:\codes\java\hive\Sort-Array-Desc\target\site</outputDirectory><reportPlugins><reportPlugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-project-info-reports-plugin</artifactId></reportPlugin></reportPlugins></configuration></execution></executions><configuration><outputDirectory>D:\codes\java\hive\Sort-Array-Desc\target\site</outputDirectory><reportPlugins><reportPlugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-project-info-reports-plugin</artifactId></reportPlugin></reportPlugins></configuration></plugin></plugins></build><reporting><outputDirectory>D:\codes\java\hive\Sort-Array-Desc\target\site</outputDirectory></reporting>
</project>

可以看到,默认配置中,指定了源代码编译的语法规则以及生成的字节码文件版本都是Java 17,与我们本地的环境不符,因此·会报错:无效的目标发行版17

此时我很奇怪为什么默认会用Java 17的语法去进行编译,于是去查看maven的配置文件settings.xml,结果发现在<profiles></profiles>中添加了一个profile

可能是之前想要控制maven的默认编译行为,结果忘记了😂

所以解决方案有两种,第一种就是删除这里的配置,由于我所使用的maven版本是3.6.3,默认就是用Java 8 进行编译了:

第二种方法就是使用maven-compiler-plugin插件配置:

    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>

从而覆盖默认的配置

3.检查IDE中的JDK 配置

除了以上Maven配置的问题,我们也需要检查IDE中的配置,下述的方法在运行代码报错时同样可用

3.1 Project Structure

进入File -> Project Structure

Project标签页中,检查 Project SDK 是否设置为 1.8:

Modules 标签页中,检查 Module SDK以及language level是否设置为 1.8:

3.2 Java Compiler

进入File -> Settings -> Java Compiler,检查Target bytecode version是否设置为1…8:

http://www.lryc.cn/news/519251.html

相关文章:

  • javafx 将项目打包为 Windows 的可执行文件exe
  • Python操作Excel的库openpyxl使用入门
  • 数据通过canal 同步es,存在延迟问题,解决方案
  • 了解Node.js
  • Android Studio创建新项目并引入第三方jar、aar库驱动NFC读写器读写IC卡
  • Oracle Dataguard(主库为双节点集群)配置详解(4):配置备库
  • 前端炫酷动画--文字(二)
  • ceph 数据均衡
  • 代码随想录算法训练营day29
  • android studio根据包名获取当前安装包信息
  • 学习第六十五行
  • 零碎的知识点(七):线性二次调节器(LQR)是什么?
  • Matlab一些使用技巧
  • Linux 发行版介绍与对比:Red Hat、Ubuntu、Kylin、Debian
  • 从CentOS到龙蜥:企业级Linux迁移实践记录(龙蜥开局)
  • java1-相对路径与绝对路径
  • iChainfo 品牌升級為 ichaingo,打造 Web3 數據基礎設施新標杆
  • Flink概念知识讲解之:Restart重启策略配置
  • [java基础-集合篇]LinkedList源码粗析
  • 面试:C++类成员初始化顺序
  • 【Python】Python与C的区别
  • [开源]自动化定位建图系统(视频)
  • ISP流程--去马赛克详解
  • Objective-C语言的软件工程
  • Objective-C语言的语法糖
  • 设计模式中的代理模式
  • 15个学习Python 的编程游戏网站
  • 微信小程序实现拖拽盒子效果
  • Linux-蓝牙协议
  • moviepy 将mp4视频文件提取音频mp3 - python 实现