到底如何配置 maven 编译插件的 JDK 版本
普通maven项目配置编译器版本
参考maven官方文档 Setting the -source
and -target
of the Java Compiler
maven有2种方法设置编译JDK版本,比如配置为 Java 1.8 版本
- 配置属性
<project> [...] <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> [...] </project>
- 直接配置插件
<project> [...] <build> [...] <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> [...] </build> [...] </project>
source属性说明:
The -source argument for the Java compiler.
NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since 3.9.0 the default value has changed from 1.6 to 1.7
Default value is:1.7
.
User property is:maven.compiler.source
.
target属性说明:
The -target argument for the Java compiler.
NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since 3.9.0 the default value has changed from 1.6 to 1.7
Default value is:1.7
.
User property is:maven.compiler.target
.
从 maven-compiler-plugin3.8.0 开始,默认java编译器版本由1.5改为1.6了
从 maven-compiler-plugin3.9.0 开始,默认java编译器版本由1.6改为1.7了
更多属性说明参见maven-compiler-plugin
SpringBoot项目配置maven编译器版本
如果引用做了父项目,可以如下配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 配置java编译器版本 --> <properties> <java.version>1.8</java.version> </properties>
配置及其简单,因为在 spring-boot-starter-parent 已经配置好了 source、target 属性,只需用 java.version 覆盖默认值即可
<properties> [...] <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> [...] </properties>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律