解决Intellij IDEA maven 自动设置JDK为JDK1.5

发布时间 2023-10-26 14:28:14作者: 青索

在idea中创建maven项目,每次更新或重新载入maven项目后,都会重新变成JDK1.5,就算手动设置maven项目或者模块JDK1.8,刷新后还是会变为JDK1.5。这是由于创建项目时没有指定jdk版本,而maven的默认jdk版本为1.5导致的。

解决方法:

1.在当前maven项目pom文件中指定编译的jdk版本(项目指定)

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

 

2.使用 maven-compiler-plugin 插件指定当前项目编译的jdk版本

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

 

3.针对于SpringBoot项目,只需指定java.version即可

<properties>
    <java.version>1.8</java.version>
</properties>

 

4. 设置 maven 全局jdk版本(针对idea配置的本地maven)

在本地maven目录下conf文件夹内的setting.xml配置文件中加入以下配置(全局配置,一劳永逸)

<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>

 


作者:angula
链接:https://juejin.cn/post/6943089876996718629
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。