Scala integration of jacoco code coverage tool in play 2.2.x project

My goal is to integrate jacoco into the project of my version 2.2.0. The various guides on the Internet that I tried to track mostly added to the confusion, rather than to closing the target.

Adding to the Confusion

  • Most guides suggest build.sbt
    • which seems to be replaced by build.scala with another
  • There is jacoco4sbt and regular jacoco
    • which is most suitable for use with scala play framework 2

Current state

added to plugins.sbt

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.2")

added to build.scala

import de.johoop.jacoco4sbt.JacocoPlugin._
lazy val jacoco_settings = Defaults.defaultSettings ++ Seq(jacoco.settings: _*)

With these changes, I do not get the "jacoco" task in sbt or in the playback console.

What are the appropriate steps to make this work?

Update On-demand contents of build.scala

import com.typesafe.sbt.SbtNativePackager._
import com.typesafe.sbt.SbtScalariform._
import play.Project._
import sbt.Keys._
import sbt._
import sbtbuildinfo.Plugin._
import de.johoop.jacoco4sbt.JacocoPlugin._

object BuildSettings {
  val buildOrganization = "XXXXX"
  val buildVersion      = "0.1"
  val buildScalaVersion = "2.10.2"
  val envConfig = "-Dsbt.log.format=false -Dconfig.file=" + Option(System.getProperty("env.config")).getOrElse("local.application")
  scalacOptions ++= Seq("-encoding", "UTF-8", "-deprecation", "-unchecked", "-feature")
  javaOptions += envConfig

  val buildSettings = Defaults.defaultSettings ++ Seq (
    organization := buildOrganization,
    version      := buildVersion,
    scalaVersion := buildScalaVersion
  )
}

object Resolvers {
  val remoteRepoUrl =  "XXXXXXXXXXXX" at "http://nexus.cXXXXX/content/repositories/snapshots/"
  val publishRepoUrl = "XXXXXXXXXXXX" at "http://nexus.ciXXXXXXXXXXXXXXXX/content/repositories/snapshots/"
}

object Dependencies {
  val ods =  "XXXXXXXXX" % "XXXXXX-ws" % "2.2.1-SNAPSHOT"
  val scalatest = "org.scalatest" %% "scalatest" % "2.0.M8" % "test"
  val mockito = "org.mockito" % "mockito-all" % "1.9.5" % "test"
}

object ApplicationBuild extends Build {

  import BuildSettings._
  import Dependencies._
  import Resolvers._

  // Sub-project specific dependencies
  val commonDeps = Seq(
    ods,
    scalatest,
    mockito
  )
  //val bN = settingKey[Int]("current build Number")
  val gitHeadCommitSha = settingKey[String]("current git commit SHA")
  val release = settingKey[Boolean]("Release")

  lazy val jacoco_settings = Defaults.defaultSettings ++ Seq(jacoco.settings: _*)

  lazy val nemo = play.Project(
    "nemo",
    path = file("."),
    settings = Defaults.defaultSettings ++ buildSettings ++
      Seq(libraryDependencies ++= commonDeps) ++
      Seq(scalariformSettings: _*) ++
      Seq(playScalaSettings: _*) ++
      buildInfoSettings ++
      jacoco_settings ++
      Seq(
        sourceGenerators in Compile <+= buildInfo,
        buildInfoKeys ++= Seq[BuildInfoKey](
          resolvers,
          libraryDependencies in Test,
          buildInfoBuildNumber,
          BuildInfoKey.map(name) { case (k, v) => "project" + k.capitalize -> v.capitalize },
          "envConfig" -> envConfig, // computed at project load time
          BuildInfoKey.action("buildTime") {
            System.currentTimeMillis
          } // re-computed each time at compile
        ),
        buildInfoPackage := "com.springer.nemo"
      ) ++
      Seq(resolvers += remoteRepoUrl) ++
      Seq(mappings in Universal ++= Seq(
        file("ops/rpm/start-server.sh") -> "start-server.sh",
        file("ops/rpm/stop-server.sh") -> "stop-server.sh"
      ))
  ).settings(version <<=  version in ThisBuild)

  lazy val nemoPackaging = Project(
    "nemoPackaging",
    file("nemoPackaging"),
    settings = Defaults.defaultSettings ++Seq(Packaging.settings:_*)
  )



  def publishSettings =
    Seq(
      publishTo := Option(publishRepoUrl),
      credentials += Credentials(
        "Repo", "http://mycompany.com/repo", "admin", "admin123"))

}

: jacoco , . :

jacoco:cover
[info] Compiling 1 Scala source to /home/schl14/work/nemo/target/scala-2.10/classes...
[info] ScalaTest
[info] Run completed in 13 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for nemo/jacoco:test
+4
3

, .

plugins.sbt

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.2")

build.scala

import de.johoop.jacoco4sbt.JacocoPlugin._

jacoco ,

lazy val xyz = play.Project(
    "xyz",
    path = file("."),
    settings = Defaults.defaultSettings
      jacoco.settings ++ //this is the important part. 
  ).settings(parallelExecution in jacoco.Config := false) //not mandatory but needed in `most cases as most test can not be run in parallel`

jacoco: sbt play, .

+4

Scala Scoverage, . https://github.com/scoverage/scalac-scoverage-plugin

/plugins.sbt:

addSbtPlugin("com.sksamuel.scoverage" % "sbt-scoverage" % "1.0.1")

SBT

sbt clean coverage test
+3

Java- jacoco4sbt, .

/plugins.sbt:

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.2")

build.sbt:

jacoco.settings

:

//or just the sbt command and then use your browser
sbt jacoco:cover && /usr/bin/x-www-browser target/scala-2.10/jacoco/html/index.html

Scala SCCT.

/plugins.sbt:

addSbtPlugin("com.github.scct" % "sbt-scct" % "0.2.1")

build.sbt:

ScctPlugin.instrumentSettings

, :

sbt scct:test && /usr/bin/x-www-browser target/scala_2.10/coverage-report/index.html

,

, -allow-file-access-from-files .

, , . firefox python -m SimpleHTTPServer 8000, http protokoll http://localhost:8000/target/scala-2.10/coverage-report/.

, , .

GitHub.

+1
source

All Articles