-->

How to set up jacoco4sbt to process classes in mai

2019-07-18 19:45发布

问题:

I'm having some problems to make jacoco4sbt working with my Play 2.3.4 project.

My project is composed of 3 submodules: common, api and frontend and has no code in the app root folder. Now when I run Jacoco it does not find the submodules classes.

Inspecting target/scala-VERSION/classes I only find some routing classes (which in fact is the only code I have in my "root" project, but I was expecting that because I aggregate all those projects the classes would be there).

If I copy the classes from MODULE_NAME/target/scala-VERSION/classes to target/scala-VERSION/classes and then run Jacoco I get the expected result.

So what is the best way to make it work? I can't find any config in jacoco4sbt to specify additional classes locations.

My build.sbt file

import Keys._

// Dummy value to deal with bug in sbt 0.13.5
val k = 0

name := "PlayApp"

version := "0.5.0"

// omitted resolvers part

scalaVersion := "2.10.4"

libraryDependencies ++= Seq(
    "com.edulify" %% "play-hikaricp" % "1.5.0" exclude("com.jolbox", "bonecp"),
    "com.novocode" % "junit-interface" % "0.11" % "test"
)

lazy val common = project.in(file("common")).enablePlugins(PlayJava)

lazy val frontend = project.in(file("frontend")).enablePlugins(PlayJava).dependsOn(common)

lazy val api = project.in(file("api")).enablePlugins(PlayJava).dependsOn(common)

lazy val main = project.in(file(".")).enablePlugins(PlayJava)
    .aggregate(frontend, api).dependsOn(frontend, api)

parallelExecution in Test := false

javaOptions in Test += "-Dconfig.resource=test.conf"

jacoco.sbt

import de.johoop.jacoco4sbt._
import JacocoPlugin._


jacoco.settings

Keys.fork in jacoco.Config := true

parallelExecution in jacoco.Config := false

jacoco.outputDirectory in jacoco.Config := file("target/jacoco")

jacoco.reportFormats in jacoco.Config := Seq(XMLReport("utf-8"), HTMLReport("utf-8"))

jacoco.excludes in jacoco.Config := Seq("views*", "*Routes*", "controllers*routes*", "controllers*Reverse*", "controllers*javascript*", "controller*ref*")

javaOptions in jacoco.Config  += "-Dconfig.resource=test.conf"

回答1:

Add jacoco.sbt to every subproject with the following content:

jacoco.settings

p.s. I've been looking for ways to convince sbt to have jacoco.settings applied to every subproject in the top-level root build.sbt, but to no avail.