解决 Maven 执行Update project后 jdk版本变回1.5的问题
默认情况下,在IDE(Eclipse或Idea)中,执行 maven -> update project 后,项目的 jdk版本变回 1.5版本。而现在绝大多数项目都是使用 jdk1.8 或 11 、17等。
因此,为解决这个问题,可以通过以下方式解决。
方法一
在maven 项目的 pom.xml中添加以下配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
这种方法只能使当前项目生效。
方法二
在maven 工具的 conf/settings.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>
如果希望使用 jdk11,则profile配置如下:
<profile> <id>jdk-11</id> <activation> <activeByDefault>true</activeByDefault> <jdk>11</jdk> </activation> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion> </properties> </profile>
注意:11 以上版本不是 1.11,而是直接是11。