2020年12月18日后SonarCloud Maven项目覆盖率配置故障求助
Let’s work through this coverage issue together—post-SonarCloud updates often break legacy Jacoco configurations, but we can get this fixed step by step.
First, Let’s Understand the Root Cause
SonarCloud deprecated the old sonar.jacoco.reportPath and sonar.jacoco.directory parameters in late 2020 in favor of XML-based coverage reports. Your current setup generates a binary .exec file, but SonarCloud now prioritizes XML reports for more reliable coverage tracking. You tried adding sonar.coverage.jacoco.xmlReportPaths, but likely missed a key step: ensuring Jacoco actually generates that XML file.
Step 1: Update Jacoco to Generate XML Reports
Your existing Jacoco configuration only produces HTML and binary reports. Add the XML format to the report execution to create the file SonarCloud needs:
<execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${sonar.jacoco.reportPath}</dataFile> <outputDirectory>${sonar.jacoco.directory}</outputDirectory> <!-- Add XML format to generate the required report --> <formats> <format>HTML</format> <format>XML</format> <format>CSV</format> </formats> </configuration> </execution>
Step 2: Replace Deprecated Sonar Parameters
Remove the old Jacoco binary report parameters and use the new XML path exclusively. Update your properties section:
<!-- Remove these deprecated lines --> <!-- <sonar.jacoco.directory>${project.testresult.directory}/coverage/jacoco</sonar.jacoco.directory> --> <!-- <sonar.jacoco.reportPath>${sonar.jacoco.directory}/jacoco.exec</sonar.jacoco.reportPath> --> <!-- Add the new XML report path (match your Jacoco output directory) --> <sonar.coverage.jacoco.xmlReportPaths>${sonar.jacoco.directory}/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
Step 3: Verify Surefire-Jacoco Integration
Your current Surefire argLine (-Dfile.encoding=UTF-8 ${surefireArgLine}) is correct, but double-check two things:
- Ensure no other Maven plugins are overriding the
surefireArgLineproperty (common with surefire/failsafe conflicts). - In your Travis CI logs, confirm the Jacoco agent is being attached—look for lines containing
jacocoagent.jarduring test execution.
Step 4: Fix Travis CI Execution Order
Make sure your Travis build runs tests (and generates Jacoco reports) before triggering SonarCloud scan. Your Maven command should look like:
mvn clean test sonar:sonar -Punit-tests
The test phase will trigger Jacoco’s prepare-agent and report executions automatically, so you don’t need a separate jacoco:report command unless you’re running it manually.
Step 5: Upgrade Sonar Maven Plugin (Optional but Recommended)
Your current sonar-maven-plugin version (3.7.0.1746) is a bit outdated. Upgrading to the latest stable version (e.g., 3.9.1.2184) can resolve compatibility issues with newer SonarCloud APIs:
<sonar-maven-plugin.version>3.9.1.2184</sonar-maven-plugin.version>
Step 6: Validate in SonarCloud
After pushing these changes, check your SonarCloud project logs (under Administration > Background Tasks) for lines like:
Importing coverage report from '/path/to/jacoco.xml'
If you see this, the report is being read successfully. If not, double-check the file path in sonar.coverage.jacoco.xmlReportPaths matches where Jacoco generates the XML file.
Final Checks
- Confirm your
sonar.exclusionsaren’t accidentally excluding classes you want coverage stats for. - Make sure no CI environment variables are overriding your Sonar properties (Travis can sometimes inject conflicting settings).
内容的提问来源于stack exchange,提问作者Stéphane GRILLON

