You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Play Framework 2.6.12 Java项目如何生成JaCoCo XML覆盖率报告?

Fixing JaCoCo XML Report Generation for Play 2.6.12 Java (for SonarQube)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:16:41