如何指定maven中java版本
使用maven-compiler-plugin插件
如下
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> -- java版本 <target>11</target> -- java版本 </configuration> </plugin> </plugins>
或者
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties>
两者是等价的
如果你使用的是idea上面的设置还能同时修改代码module里的language level。
使用spring boot时
如果pom.xml继承了spring-boot-starter-parent
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>xxxx</version> <relativePath/> </parent>
还可以使用如下方式来指定java版本
<properties> <java.version>11</java.version> -- java版本 </properties>
底层其实也是定义了 maven.compiler.source 和 maven.compiler.target 两个属性
<?xml version="1.0" encoding="utf-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.1.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent> <artifactId>spring-boot-starter-parent</artifactId> <packaging>pom</packaging> <name>Spring Boot Starter Parent</name> <description>Parent pom providing dependency and plugin management for applications built with Maven</description> <url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url> <properties> <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> </project>
参考
https://stackoverflow.com/questions/38882080/specifying-java-version-in-maven-differences-between-properties-and-compiler-p
https://stackoverflow.com/questions/54467287/how-to-specify-java-version-in-spring-spring-boot-pom-xml
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异