我发现JaCoCo Code Coverage plug-in for sbt似乎非常适合我的需求,但我在设置时遇到了问题。维基表示只需要将两行添加到project/plugins.sbt
和build.sbt
,但我执行sbt jacoco:cover
时出现以下错误:
bhan@bhan-laptop:~/src/connectifier$ sbt jacoco:cover
[info] Loading project definition from /home/bhan/src/connectifier/project
[info] Set current project to connectifier (in build file:/home/bhan/src/connectifier/)
[info] Set current project to connectifier (in build file:/home/bhan/src/connectifier/)
[error] No such setting/task
[error] jacoco:cover
[error]
bhan@benhan-laptop:~/src/connectifier$ sbt about
[info] Loading project definition from /home/bhan/src/connectifier/project
[info] Set current project to connectifier (in build file:/home/bhan/src/connectifier/)
[info] Set current project to connectifier (in build file:/home/bhan/src/connectifier/)
[info] This is sbt 0.13.6
[info] The current project is {file:/home/bhan/src/connectifier/}frontend 1.0-SNAPSHOT
[info] The current project is built against Scala 2.11.1
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, play.Play, play.PlayJava, play.PlayScala, play.twirl.sbt.SbtTwirl, com.typesafe.sbt.jse.SbtJsEngine, com.typesafe.sbt.jse.SbtJsTask, com.typesafe.sbt.web.SbtWeb, com.typesafe.sbt.webdriver.SbtWebDriver, com.typesafe.sbt.rjs.SbtRjs, com.typesafe.sbt.less.SbtLess, com.typesafe.sbt.digest.SbtDigest, com.typesafe.sbt.gzip.SbtGzip, com.typesafe.sbteclipse.plugin.EclipsePlugin, org.sbtidea.SbtIdeaPlugin, com.typesafe.sbt.SbtNativePackager, de.johoop.jacoco4sbt.JacocoPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
我错过了任何其他设置吗?我对sbt / activator不是很熟悉,但我的印象是sbt会自动从相关的repo下载插件,并且我不需要手动克隆git repo。这是对的吗?
答案 0 :(得分:1)
tl; dr 尝试重现我的样本中的问题并没有解决问题。 jacoco4sbt插件的文档工作正常。
查看build.sbt
和project/plugins.sbt
确实很有帮助。
构建似乎是安装了sbt-native-packager的Play项目(在其他插件中)。
以下是我在构建中的工作原理:
➜ sandbox activator new jacoco4sbt-play play-scala
Fetching the latest list of templates...
OK, application "jacoco4sbt-play" is being created using the "play-scala" template.
To run "jacoco4sbt-play" from the command line, "cd jacoco4sbt-play" then:
/Users/jacek/sandbox/jacoco4sbt-play/activator run
To run the test for "jacoco4sbt-play" from the command line, "cd jacoco4sbt-play" then:
/Users/jacek/sandbox/jacoco4sbt-play/activator test
To run the Activator UI for "jacoco4sbt-play" from the command line, "cd jacoco4sbt-play" then:
/Users/jacek/sandbox/jacoco4sbt-play/activator ui
➜ sandbox cd jacoco4sbt-play
➜ jacoco4sbt-play tree
.
|-- LICENSE
|-- README
|-- activator
|-- activator-launch-1.2.10.jar
|-- app
| |-- controllers
| | `-- Application.scala
| `-- views
| |-- index.scala.html
| `-- main.scala.html
|-- build.sbt
|-- conf
| |-- application.conf
| `-- routes
|-- project
| |-- build.properties
| `-- plugins.sbt
|-- public
| |-- images
| | `-- favicon.png
| |-- javascripts
| | `-- hello.js
| `-- stylesheets
| `-- main.css
`-- test
|-- ApplicationSpec.scala
`-- IntegrationSpec.scala
10 directories, 17 files
➜ jacoco4sbt-play cat project/plugins.sbt
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.4")
// web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.0.0")
addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.8.0-M1")
➜ jacoco4sbt-play cat build.sbt
name := """jacoco4sbt-play"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws
)
jacoco.settings
➜ jacoco4sbt-play sbt jacoco:cover
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Loading project definition from /Users/jacek/sandbox/jacoco4sbt-play/project
[info] Updating {file:/Users/jacek/sandbox/jacoco4sbt-play/project/}jacoco4sbt-play-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] downloading http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/com.typesafe.sbt/sbt-native-packager/scala_2.10/sbt_0.13/0.8.0-M1/jars/sbt-native-packager.jar ...
[info] [SUCCESSFUL ] com.typesafe.sbt#sbt-native-packager;0.8.0-M1!sbt-native-packager.jar (5065ms)
[info] Done updating.
[info] Set current project to jacoco4sbt-play (in build file:/Users/jacek/sandbox/jacoco4sbt-play/)
[info] Updating {file:/Users/jacek/sandbox/jacoco4sbt-play/}root...
[info] Resolving org.jacoco#org.jacoco.agent;0.7.1.201405082137 ...
[info] Done updating.
[info] Compiling 5 Scala sources and 1 Java source to /Users/jacek/sandbox/jacoco4sbt-play/target/scala-2.11/classes...
[info] Compiling 2 Scala sources to /Users/jacek/sandbox/jacoco4sbt-play/target/scala-2.11/test-classes...
[info] ApplicationSpec
[info]
[info] Application should
[info] + send 404 on a bad request
[info] + render the index page
[info]
[info] Total for specification ApplicationSpec
[info] Finished in 22 ms
[info] 2 examples, 0 failure, 0 error
[info] IntegrationSpec
[info]
[info] Application should
[info] + work from within a browser
[info]
[info] Total for specification IntegrationSpec
[info] Finished in 22 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[info]
[info] ------- Jacoco Coverage Report --------
[info]
[info] Lines: 57.5% (>= required 0.0%) covered, 34 of 80 missed, OK
[info] Instructions: 71.94% (>= required 0.0%) covered, 523 of 1864 missed, OK
[info] Branches: 27.78% (>= required 0.0%) covered, 26 of 36 missed, OK
[info] Methods: 81.94% (>= required 0.0%) covered, 41 of 227 missed, OK
[info] Complexity: 76.73% (>= required 0.0%) covered, 57 of 245 missed, OK
[info] Class: 57.14% (>= required 0.0%) covered, 12 of 28 missed, OK
[info] Check /Users/jacek/sandbox/jacoco4sbt-play/target/scala-2.11/jacoco for detail report
[info]
[success] Total time: 16 s, completed Sep 23, 2014 10:45:36 PM
工作正常。他们正确地指望人们在构建中使用这两行来正确设置插件。
我可以看到de.johoop.jacoco4sbt.JacocoPlugin
确实列在sbt about
的插件部分中。
我在构建中也有一组非常相似的插件。
➜ jacoco4sbt-play sbt about
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Loading project definition from /Users/jacek/sandbox/jacoco4sbt-play/project
[info] Set current project to jacoco4sbt-play (in build file:/Users/jacek/sandbox/jacoco4sbt-play/)
[info] This is sbt 0.13.5
[info] The current project is {file:/Users/jacek/sandbox/jacoco4sbt-play/}root 1.0-SNAPSHOT
[info] The current project is built against Scala 2.11.1
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, play.Play, play.PlayJava, play.PlayScala, play.twirl.sbt.SbtTwirl, com.typesafe.sbt.jse.SbtJsEngine, com.typesafe.sbt.jse.SbtJsTask, com.typesafe.sbt.web.SbtWeb, com.typesafe.sbt.webdriver.SbtWebDriver, com.typesafe.sbt.coffeescript.SbtCoffeeScript, com.typesafe.sbt.less.SbtLess, com.typesafe.sbt.jshint.SbtJSHint, com.typesafe.sbt.rjs.SbtRjs, com.typesafe.sbt.digest.SbtDigest, com.typesafe.sbt.mocha.SbtMocha, com.typesafe.sbteclipse.plugin.EclipsePlugin, net.virtualvoid.sbt.graph.Plugin, com.timushev.sbt.updates.UpdatesPlugin, org.sbtidea.SbtIdeaPlugin, com.typesafe.sbt.SbtNativePackager, de.johoop.jacoco4sbt.JacocoPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
在项目中使用以下build.sbt
,请注意文件中的最后一行:
lazy val data = project
lazy val avro = project
lazy val frontend = (project in file("frontend"))
.enablePlugins(PlayJava, SbtWeb)
.dependsOn(data % "test->test;compile->compile").aggregate(data)
.dependsOn(avro).aggregate(avro)
// JaCoCo (java) test coverage
jacoco.settings
// Scoverage (scala) test coverage
ScoverageSbtPlugin.instrumentSettings
// Switch to the frontend project on startup
onLoad in Global := { Command.process("project frontend", _: State) } compose (onLoad in Global).value
在最后一行,您切换到没有启用jacoco4sbt插件的项目frontend
。
执行project
命令查看您所处的项目。我确定它将成为frontend
。
> project
[info] frontend (in build file:/Users/jacek/sandbox/jacoco4sbt-play/)
我的回答考虑了单个项目构建,而您正在使用3模块构建,只使用jacoco4sbt设置隐式根项目。
最简单的解决方案是使用以下内容在build.sbt
目录中创建frontend
:
jacoco.settings
执行reload
(或停止并启动sbt)时,jacoco:cover
应该可以正常工作。
> project
[info] frontend (in build file:/Users/jacek/sandbox/jacoco4sbt-play/)
> jacoco:cover
[info] Updating {file:/Users/jacek/sandbox/jacoco4sbt-play/}frontend...
[info] Resolving org.jacoco#org.jacoco.agent;0.7.1.201405082137 ...
[info] Done updating.
[info]
[info] ------- Jacoco Coverage Report --------
[info]
[info] Lines: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Instructions: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Branches: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Methods: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Complexity: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Class: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Check /Users/jacek/sandbox/jacoco4sbt-play/frontend/target/scala-2.10/jacoco for detail report
[info]
[success] Total time: 0 s, completed Sep 26, 2014 5:35:06 PM