升级Java17+SpringBoot3.1.4后Jenkins构建报错:Maven Compiler插件不支持
问题背景
本地已将SpringBoot项目升级至Java17和SpringBoot3.1.4版本(适配Spring Security6.x+),可正常完成代码构建、测试及发布,但Jenkins Pipeline执行构建时失败,报错:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project uni-dashboard-portal-external: Fatal error compiling: error: release version 17 not supported
已升级Jenkins Agent,但问题依旧。项目pom.xml相关配置如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.4</version> <relativePath/> </parent> <properties> <java.version>17</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.release>${java.version}</maven.compiler.release> <source.version>${java.version}</source.version> </properties> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <release>${java.version}</release> <target>${java.version}</target> <compilerArgs> <arg>-Xlint:all</arg> </compilerArgs> </configuration> </plugin>
排查与解决步骤
确认Jenkins Agent的Java版本实际生效
不要仅依赖Agent的安装记录,直接在Pipeline中执行java -version和mvn -v命令,输出当前构建环境使用的Java版本。多数情况是Agent配置了多版本JDK,但Pipeline未指定使用Java17,仍默认调用低版本JDK。
需在Jenkins全局工具配置中添加Java17的JDK,然后在Pipeline脚本中通过tool指令指定:pipeline { agent any tools { jdk 'Java17' // 与Jenkins全局配置的JDK名称一致 } stages { stage('Build') { steps { sh 'mvn clean compile' } } } }检查Maven版本兼容性
maven-compiler-plugin 3.11.0支持Java17,但要求Maven版本不低于3.8.1(Maven 3.8.x起对Java17支持更完善)。执行mvn -v确认版本,若过低需升级Agent中的Maven。
同时简化pom的编译器配置:Spring Boot parent已默认配置编译参数,无需重复设置source、target、release,避免冲突,简化后插件配置如下:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <compilerArgs> <arg>-Xlint:all</arg> </compilerArgs> </configuration> </plugin>验证Agent的环境变量
确认Agent的JAVA_HOME环境变量指向Java17安装目录,部分场景下即使安装了Java17,JAVA_HOME仍指向旧版本,导致Maven使用错误JDK编译。
内容的提问来源于stack exchange,提问作者user1477720

