Play Framework 2.6.12 Java项目如何生成JaCoCo XML覆盖率报告?
I feel your pain—dealing with unmaintained plugins while trying to get coverage reports for SonarQube is no fun. Let's get this sorted step by step, since you're on Play 2.6.12 (Java) and need a working JaCoCo setup that outputs XML.
Step 1: Replace the Unmaintained JaCoCo SBT Plugin
First, ditch the old, unupdated JaCoCo plugin and use the actively maintained sbt-jacoco plugin. Update your project/plugins.sbt file with this line:
addSbtPlugin("com.github.sbt" % "sbt-jacoco" % "3.4.0")
This version is compatible with the SBT versions used by Play 2.6.x, and it’s actively maintained to avoid the stale repository issues you ran into.
Step 2: Configure JaCoCo to Generate XML Reports
Next, tweak your build.sbt to tell JaCoCo to output XML (the format SonarQube requires) alongside optional HTML for human-readable reports. Add these settings:
// Enable core JaCoCo settings for the project jacoco.settings // Configure report formats—keep HTML if you want a browsable report, or just use Xml jacocoReportSettings := JacocoReportSettings() .withFormats(JacocoReportFormats.Xml, JacocoReportFormats.Html) .withTitle("Play 2.6.12 Java Project Coverage") // Optional: Exclude auto-generated or non-business logic files (adjust as needed) jacocoExcludes := Seq("router/**", "views/**", "controllers/routes/**")
If you only need XML for SonarQube, remove JacocoReportFormats.Html from the formats list.
Step 3: Generate the Coverage Report
Run this command to clean existing builds, execute tests, and generate the JaCoCo report:
sbt clean test jacoco
Once it finishes, you’ll find the XML report at target/jacoco/report/jacoco.xml—this is the exact file SonarQube needs.
Step 4: Point SonarQube to the XML Report
Update your sonar-project.properties file (create it in your project root if missing) to reference the JaCoCo XML report:
sonar.projectKey=your-unique-project-key sonar.projectName=Your Play 2.6.12 Java Project sonar.java.coveragePlugin=jacoco sonar.jacoco.reportPaths=target/jacoco/report/jacoco.xml # Add other required SonarQube settings sonar.sources=app sonar.java.binaries=target/scala-2.12/classes
Troubleshooting Tip
If JaCoCo fails to capture coverage, ensure tests are forked so the JaCoCo agent attaches properly. Add this to build.sbt:
fork in Test := true javaOptions in Test ++= Seq( "-Djacoco-agent.destfile=target/jacoco/jacoco.exec" )
内容的提问来源于stack exchange,提问作者El Mac

