升级Spring Boot后Sonar Gradle任务因Jackson SNAKE_CASE属性缺失失败
问题
在Vela环境中执行Sonar Gradle任务时失败,报错:
No such property: SNAKE_CASE for class: com.fasterxml.jackson.databind.PropertyNamingStrategy
关键信息
- 代码库仅使用
PropertyNamingStrategies,未直接调用PropertyNamingStrategy - 本地构建正常,Spring Boot升级至3.5.13及以上版本时触发问题,3.5.12及以下版本无异常
- 环境配置:
- Java 17
- Gradle 8.x
- SonarQube Gradle插件7.2.2
- Jackson 2.21.x
- 所有外部依赖均使用Jackson 2.19及以上版本
Vela构建步骤配置
- name: build ruleset: matcher: regexp branch: main|[0-9].x event: [ push, pull_request ] image: docker.xxx.com/toolshed/base-jdk:17-jdk-linux-amd64 secrets: [ sonar_token ] environment: GRADLE_USER_HOME: .gradle GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false commands: - adduser --disabled-password postgres - chown postgres "/vela/src" -R - sed -i -e "s/INSERT_TOKEN_HERE/$SONAR_TOKEN/g" gradle.properties - su postgres -c "export JAVA_HOME=/opt/java/openjdk; cd /vela/src/git.xxx.com/hesperides/${REPOSITORY_NAME}; ./gradlew clean build sonar"
build.gradle核心配置
plugins { // ...其他插件 id "org.sonarqube" version 7.2.2.6593 // ...其他插件 } dependencies { implementation "org.springframework.boot:spring-boot-starter-actuator" implementation "org.springframework.boot:spring-boot-starter-webflux" // ...其他依赖 } sonarqube { properties { property "sonar.projectVersion", "1" property "sonar.sourceEncoding", "UTF-8" property "sonar.java.coveragePlugin", "jacoco" property "sonar.jacoco.reportPath", "build/jacoco/test.exec" // ...其他Sonar配置 } }
完整报错日志
> Task :sonar Both 'sonar.login' and 'sonar.token' (or the 'SONAR_TOKEN' env variable) are set, but only the latter will be used. Use of 'sonar.login' property has been deprecated in favor of 'sonar.token' (or the env variable alternative 'SONAR_TOKEN'). Please use the latter when passing a token. [stderr] Picked up JAVA_TOOL_OPTIONS: -javaagent:/app/otel-xxx-extensions.jar [stderr] OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended [stderr] Picked up JAVA_TOOL_OPTIONS: -javaagent:/app/otel-xxx-extensions.jar [stderr] OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended The property 'sonar.login' is deprecated and will be removed in the future. Please use the 'sonar.token' property instead when passing a token. Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them. Use of preview features have been detected during analysis. Enable DEBUG mode to see them. Property 'sonar.jacoco.reportPath' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin. View Projektor report at: https://projektor.dev.xxx.com/tests/OJJ3W4FORXAH FAILURE: Build failed with an exception. * What went wrong: No such property: SNAKE_CASE for class: com.fasterxml.jackson.databind.PropertyNamingStrategy
分析与解决方案
这是SonarQube Gradle插件旧版本与Jackson 2.19+版本的兼容性问题,原因如下:
Jackson从2.19版本开始,将PropertyNamingStrategy类中的常量(如SNAKE_CASE)全部迁移到了PropertyNamingStrategies类中,高版本Jackson(如2.21)已彻底移除了旧类中的常量引用。而你使用的SonarQube Gradle插件7.2.2版本,内部仍依赖旧的PropertyNamingStrategy.SNAKE_CASE调用,导致Jackson版本升级后触发找不到属性的错误。
解决步骤
升级SonarQube Gradle插件
将插件版本升级到7.3.0及以上(该版本已适配Jackson 2.19+的API变更),修改build.gradle中的插件配置:plugins { id "org.sonarqube" version "7.3.0.5839" // 或更高稳定版 }临时兼容方案(不推荐长期使用)
如果暂时无法升级Sonar插件,可在buildscript的依赖中强制指定兼容的Jackson版本,让Sonar插件使用旧版Jackson:buildscript { dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" // 强制Sonar插件使用Jackson 2.18.x版本 classpath("com.fasterxml.jackson.core:jackson-databind:2.18.3") { force = true } } }注意:此方案可能导致项目其他部分的Jackson依赖冲突,仅作为临时过渡使用。
清理废弃配置
报错中提示sonar.jacoco.reportPath已废弃,建议替换为JaCoCo XML报告配置:sonarqube { properties { // 移除旧配置 // property "sonar.jacoco.reportPath", "build/jacoco/test.exec" // 添加新配置 property "sonar.jacoco.xmlReportPaths", "build/reports/jacoco/test/jacocoTestReport.xml" } }同时确保JaCoCo任务生成XML报告:
jacocoTestReport { reports { xml.required = true } } // 让Sonar任务依赖JaCoCo报告生成任务 tasks.sonar.dependsOn jacocoTestReport
内容的提问来源于stack exchange,提问作者Geogrio
相关产品推荐
相关产品推荐

