【问题标题】:How to set up Jacoco with activator/sbt?如何使用激活器/sbt 设置 Jacoco?
【发布时间】:2014-11-09 15:08:10
【问题描述】:

我发现JaCoCo Code Coverage plug-in for sbt 似乎非常适合我的需求,但我在设置它时遇到了问题。 wiki 指出只需要在project/plugins.sbtbuild.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。这是正确的吗?

【问题讨论】:

    标签: sbt jacoco typesafe-activator


    【解决方案1】:

    tl;dr 尝试在我的示例中重现该问题并没有成功。 jacoco4sbt 插件的文档运行良好。

    看到build.sbtproject/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 设置隐式根项目。

    最简单的解决方案是在frontend 目录中创建build.sbt,并使用以下内容:

    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
    

    【讨论】:

    • 感谢您的明确指示。我不熟悉 sbt/activator 并且缺少 new jacoco4sbt-play play-scala。激活器测试似乎正在工作,但我仍然收到jacoco:cover 的以下错误:Not a valid key: cover (similar: resolvers, compilers, deliver)
    • 还有一些问题,这里是 build.sbt 和 plugins.sbt:pastebin.com/iC6ku385
    • 查看我的更新。它应该可以解决您的问题。感谢您接受答案!
    猜你喜欢
    • 2014-09-25
    • 2014-10-08
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2016-02-10
    相关资源
    最近更新 更多