idea修改language level版本实现方式
作者:杰杰程序猿1024
文章介绍了在不同版本JDK之间的切换时遇到的语言级别版本问题的解决方案,通过修改系统环境变量、Maven配置和IDEA设置,可以实现不同JDK版本的切换,并确保项目顺利编译和运行
1、前言
使用场景,当原本JDK1.8版本的编译环境时,突然加载一个JDK17的项目时,项目reload或maven install时经常报错language level版本问题,以下是解决方案。
2、操作步骤
2.1 修改系统环境变量
多个JDK为了方便切换,我的环境变量是这样设置的,JAVA_HOME负责切换版本,Path变量就跟平时配置的一样。
需要到哪个版本就切换JAVA_HOME里面的变量就行。

2.2 修改MAVEN的配置
maven的配置文件在安装目录的setting.xml,需配置多个JDK的配置。
<profiles>里面配置多个JDK,<activeProfiles>里面负责切换配置。
如下方配置:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>aliyun-public</name>
<url>https://maven.aliyun.com/repository/public/</url>
</mirror>
<mirror>
<id>aliyun-spring</id>
<mirrorOf>spring</mirrorOf>
<name>aliyun-spring</name>
<url>https://maven.aliyun.com/repository/spring</url>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>one of the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://repo1.maven.org/maven2/</url>
</mirror>
</mirrors>
<!-- 以下是JDK配置 -->
<profiles>
<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>
<profile>
<id>jdk-17</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>17</jdk>
</activation>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<!-- <activeProfile>jdk-1.8</activeProfile>-->
<activeProfile>jdk-17</activeProfile>
</activeProfiles>
</settings>
2.3 idea配置修改
(1)模块设置菜单。

(2)模块设置jdk。

(3)idea设置:file->setting,把图片中的修改为你要的JDK版本。

(4)重载maven

(5)校验,跟(1)的步骤一样进入model setting,如下图所示:

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
