Maven 设置 JDK 版本

指定 source 和 target 参数

javac 支持通过 -source-target 参数指定源码语法版本和编译产物 JVM 兼容版本。在 Maven 项目中可以通过 Apache Maven Complier Plugin 插件传递这两个参数。

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

等价于直接配置插件:

<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>
  • source:指定源码使用的语言特性版本
  • target:指定编译产物的 JVM 兼容版本

参见:Setting the -source and -target of the Java Compiler | Apache Maven

指定 release 参数(推荐)

从 JDK 9 开始, javac 支持通过 --release 参数指定目标 Java 版本。相比于 -source-target 参数,--release 参数还会在源码使用了目标 Java 版本中不存在的 API 时产生错误。

从 Apache Maven Complier Plugin v3.6 开始支持传递 release 参数:

<properties>
  <maven.compiler.release>8</maven.compiler.release>
</properties>

等价于直接配置插件:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.12.1</version>
  <configuration>
    <release>8</release>
  </configuration>
</plugin>

参考:Setting the --release of the Java Compiler | Apache Maven

posted @ 2024-02-23 18:12  Undefined443  阅读(1961)  评论(0)    收藏  举报