Gradle 8.5运行SonarQube报错,疑build.gradle配置有问题
Gradle 8.5 执行SonarQube任务报错排查与解决
问题场景
使用Gradle 8.5执行SonarQube扫描任务时失败,错误提示为'java.io.File org.gradle.api.tasks.testing.JUnitXmlReport.getDestination()',推测是SonarQube插件版本不兼容或配置有误导致。
执行的命令
./gradlew sonarqube -Dsonar.projectKey=${VAR_SONAR_PROJECT_NAME} -Dsonar.projectName=${VAR_SONAR_PROJECT_NAME} -Dsonar.host.url="${VAR_SONAR_URL}" -Dsonar.login=${VAR_SONAR_TOKEN} -Dsonar.qualitygate.wait=true
错误日志
Downloading https://services.gradle.org/distributions/gradle-8.5-bin.zip ............10%.............20%............30%.............40%........ .....50%............60%.............70%.............80%............90%.............100% Welcome to Gradle 8.5! Here are the highlights of this release: - Support for running on Java 21 - Faster first use with Kotlin DSL - Improved error and warning messages For more details see https://docs.gradle.org/8.5/release-notes.html Starting a Gradle Daemon (subsequent builds will be faster) > Task :buildSrc:extractPluginRequests > Task :buildSrc:generatePluginAdapters > Task :buildSrc:compileJava > Task :buildSrc:compileGroovy NO-SOURCE > Task :buildSrc:compileGroovyPlugins > Task :buildSrc:pluginDescriptors > Task :buildSrc:processResources > Task :buildSrc:classes > Task :buildSrc:jar > Task :list:compileJava > Task :utilities:compileJava > Task :app:compileJava > Task :app:processResources NO-SOURCE > Task :app:classes > Task :app:compileTestJava > Task :app:sonarqube FAILURE: Build failed with an exception. * What went wrong: 'java.io.File org.gradle.api.tasks.testing.JUnitXmlReport.getDestination()' * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 1m 31s 11 actionable tasks: 11 executed
现有build.gradle配置(app模块)
plugins { id 'demo.app.java-application-conventions' id 'war' id "org.sonarqube" version "3.3" id 'jacoco' } dependencies { implementation 'org.apache.commons:commons-text' implementation project(':utilities') } application { // Define the main class for the application. mainClass = 'demo.app.app.App' } sonarqube { properties { property "sonar.module", "$project.name" property "sonar.sources", "app/src/main" property "sonar.tests", "app/src/test" property "sonar.java.binaries", "build/classes" property "sonar.java.coveragePlugin", "jacoco" } }
解决步骤
1. 升级SonarQube插件版本
SonarQube 3.3版本发布于2021年,与Gradle 8.5存在API兼容性问题——Gradle 8.x对JUnitXmlReport.getDestination()的返回类型做了调整,旧版本插件调用了已过时的API,导致报错。
将插件版本升级到兼容Gradle 8.5的稳定版本,例如4.4.1.3373:
plugins { // ...其他插件 id "org.sonarqube" version "4.4.1.3373" // ...其他插件 }
2. 清理冗余配置,依赖插件自动检测
SonarQube Gradle插件已能自动识别项目结构,无需手动指定大部分路径:
sonar.module:该属性已被弃用,模块标识由插件自动处理sonar.sources/sonar.tests:插件会自动检测主源码和测试源码目录sonar.java.binaries:插件会自动识别编译后的class文件路径
简化后的sonarqube配置:
sonarqube { properties { property "sonar.java.coveragePlugin", "jacoco" // 若需自定义模块唯一标识,可添加sonar.projectKey(命令行已指定全局key,模块会自动继承并追加标识) // property "sonar.projectKey", "${VAR_SONAR_PROJECT_NAME}-${project.name}" } }
3. 确保Jacoco覆盖率报告生成(可选)
若需要同步覆盖率数据到SonarQube,需确保在执行sonarqube任务前先生成Jacoco报告:
./gradlew test jacocoTestReport sonarqube -Dsonar.projectKey=${VAR_SONAR_PROJECT_NAME} ...
也可在build.gradle中添加任务依赖,自动触发报告生成:
tasks.sonarqube.dependsOn(tasks.jacocoTestReport)
内容的提问来源于stack exchange,提问作者vinod827
相关产品推荐
相关产品推荐

