maven在settings.xm和pom.xml中指定jdk版本编译
2025-06-24 12:30:55 | 来源:人民网

maven的settings.xm和pom.xml都可以通过 maven.compiler.source
, maven.compiler.target
这两个属性值来指定jdk版本
maven.compiler.source
maven.compiler.target
maven.compiler.source
maven.compiler.target
在pom.xml中的位置
<project><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>project>
在settings.xml中的位置
<settings><profiles><profile><activation><activeByDefault>trueactiveByDefault>activation><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>profile>profiles>settings>
在spring项目中, 用java.version
来统一设置
maven的settings.xm和pom.xml也可以通过设定 maven-compiler-plugin
这个插件来指定jdk版本
<plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><version>3.9.6version><configuration><source>21source><target>21target>configuration>plugin>
在pom.xml中的位置
<project>... <build>... <plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><version>3.9.6version><configuration><source>21source><target>21target>configuration>plugin>plugins>... build>...project>
在settings.xml中的位置 , 好像用不了
<settings>... <profiles><profile><id>profile-maven-compiler-pluginid><activation><activeByDefault>trueactiveByDefault>activation><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><version>3.9.6version><configuration><source>17source><target>17target>configuration>plugin>plugins>build>profile>profiles>...settings>
Maven 在 settings.xml 中指定jdk版本
settings.xml 中的属性写在 setting👉profiles👉profile👉properties中,位于第5层
方法一, 直接写死, 例如指定jdk21
<settings><profiles><profile><id>jdk-version-21id><activation><activeByDefault>trueactiveByDefault>activation><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>profile>profiles><activeProfiles><activeProfile>jdk-version-21activeProfile>activeProfiles>settings>
去掉注释
<profiles><profile><id>jdk-version-21id><activation><activeByDefault>trueactiveByDefault>activation><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>profile>profiles><activeProfiles><activeProfile>jdk-version-21activeProfile>activeProfiles>
只用
激活, 可以不要
和
<profiles><profile><activation><activeByDefault>trueactiveByDefault>activation><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>profile>profiles>
只用
激活 , 则可以不要
<profiles><profile><id>jdk-version-21id><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>profile>profiles><activeProfiles><activeProfile>jdk-version-21activeProfile>activeProfiles>
引用属性变量,只在一个地方修设值jdk版本
<settings><profiles><profile><id>set-jdk-versionid><activation><activeByDefault>trueactiveByDefault>activation><properties><jdk-version>21jdk-version><maven.compiler.source>${ jdk-version}maven.compiler.source><maven.compiler.target>${ jdk-version}maven.compiler.target>properties>profile>profiles><activeProfiles><activeProfile>set-jdk-versionactiveProfile>activeProfiles>settings>
一处设置,双重激活
<profiles><profile><id>set-JdkVersionid><activation><activeByDefault>trueactiveByDefault>activation><properties><JdkVersion>21JdkVersion><maven.compiler.source>${ JdkVersion}maven.compiler.source><maven.compiler.target>${ JdkVersion}maven.compiler.target>properties>profile>profiles><activeProfiles><activeProfile>set-JdkVersionactiveProfile>activeProfiles>
Maven 在 pom.xml 中指定jdk版本
在pom.xml中可以用设置属性或者设置插件两种方法来设置jdk版本
- 用设置属性的方式
<project><properties><maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>properties>project>
<maven.compiler.source>21maven.compiler.source><maven.compiler.target>21maven.compiler.target>
- 用设置插件的方式 , 设置插件的方式优先级高于设置属性
<project>... <build>... <plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><configuration><source>21source><target>21target>configuration>plugin>plugins>... build>...project>
- 用设置插件的方式 , 设置插件的方式优先级高于设置属性
<plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><configuration><source>21source><target>21target>configuration>plugin>plugins>
两种方法都用上, , 插件的优先级高于属性
<properties><project.build.sourceEncoding>UTF-8project.build.sourceEncoding><JdkVersionOfThisPom>17JdkVersionOfThisPom><java.version>${ JdkVersionOfThisPom}java.version><maven.compiler.source>${ JdkVersionOfThisPom}maven.compiler.source><maven.compiler.target>${ JdkVersionOfThisPom}maven.compiler.target><maven.compiler.compilerVersion>${ JdkVersionOfThisPom}maven.compiler.compilerVersion>properties><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><configuration><source>${ JdkVersionOfThisPom}source><target>${ JdkVersionOfThisPom}target><compilerVersion>${ JdkVersionOfThisPom}compilerVersion>configuration>plugin>plugins>build>
(责编:人民网)
分享让更多人看到
热门排行
- 1AI生成图像检测调研
- 2如何将图片或图片地址存储到 MySQL 以及如何通过数据库中的这些图像数据 JSP 显示在网页上
- 3只看这篇文章就够了:0经验Windows部署DeepSekek Docker Dify实现个人/企业RAG知识库,安装和配置保姆级教程
- 4九方智能投资控股新产品发布,易知股道引领智能投资新时代!
- 5基于Java 内蒙古呼和浩特城市文化展示系统的设计与实现
- 6拼多多推广新的“净成交出价”:帮助商家降低秒退订单的成本
- 7关于c++的几个简单算法 & csp
- 8AIGC个性化定制内容生成:技术与应用的前沿探索
- 9Redis 如何保证数据一致性:从原理到实践的综合分析
- 10如何接入AI音乐生成接口(Suno接口//API)