maven jdk21配置
时间: 2024-08-12 16:05:48 浏览: 129
Maven是一个流行的项目管理和构建工具,它支持多种Java版本。JDK 21(实际上,截至2023年,JDK 21尚未发布,最新的稳定版是JDK 20)需要在Maven项目中进行配置。以下是基本步骤:
1. **安装JDK 20或更新版本**:首先,你需要下载并安装最新版的JDK(比如JDK 20),确保安装路径添加到系统的PATH环境变量中。
2. **设置Maven**:如果你已经安装了Maven,确保其使用的默认JDK版本与新安装的JDK相匹配。打开`settings.xml`文件(通常位于`<MAVEN_HOME>/conf`目录下),检查 `<activeProfiles>` 标签,确认`maven-compiler-plugin` 配置的 `jdkVersion` 设置为 "1.8" 或者更高的版本(如 "1.20"),如果还未配置,则添加类似这样的条目:
```xml
<profile>
<id>java-21</id>
<activation>
<jdk>[1.20,)</jdk>
</activation>
<properties>
<maven.compiler.source>1.20</maven.compiler.source>
<maven.compiler.target>1.20</maven.compiler.target>
</properties>
</profile>
```
3. **启用特定版本**:在`pom.xml`文件中,如果你想在某个模块上使用JDK 21,可以指定`<build>`标签下的`plugins`部分,使用`maven-compiler-plugin`的`<version>`属性指明特定的插件版本,例如:
```xml
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- 使用支持JDK 21的版本 -->
<configuration>
...
<source>1.20</source>
<target>1.20</target>
</configuration>
</plugin>
</plugins>
...
</build>
```
4. **验证配置**:最后,在命令行运行`mvn help:effective-pom`来查看实际生效的配置,确认`maven-compiler-plugin`的目标和源版本是否已正确设置为JDK 21。
阅读全文
相关推荐


















