9

WartRemover是一个 scalac 插件。通常它是通过一个sbt 插件配置的。

我希望能够在我的 sbt 项目上将 WartRemover 作为单独的配置或任务运行,而不会影响compile.

在将 Wartrremover 添加到我的之后,plugins.sbt我尝试了以下几种变体。

lazy val Lint = config("lint").extend(Compile)

project("foo").
  configs(Lint)
  settings(inConfig(Lint)(Defaults.compileSettings): _*).
  settings(inConfig(Linting)(wartremover.wartremoverSettings):_*).
  settings(inConfig(Linting)(wartremover.wartremoverErrors := wartremover.Warts.all))

之后scalacOptions大致包含了我在新lint配置和配置中的预期compile。但是,当我在调试模式下运行lint:compilecompile使用 sbt 时,我可以看到 scalac 的命令行参数,两个或两个命令都不会导致通过-P:wartremover:...开关。这很令人惊讶,因为只lint:scalacOptions显示了-P:wartremover:...开关。

如何创建单独的 sbt 配置或任务以使用 WartRemover 进行编译而不影响compile:compile

4

1 回答 1

11

我想你真的很接近。以下是一些细节:

  1. sbt 下载库依赖项和编译器插件都使用Compile配置的update任务,该任务使用libraryDependencies设置。addCompilerPluginlibraryDependencieswithCompilerPlugin配置的简写。
  2. 编译器插件需要scalaOptions您感兴趣的配置。
  3. 您需要从中获取sources才能CompileLint.

如果您看到wartremoverSettings它的实现同时执行addCompilerPluginscalacOptions. 您有两个选项可以禁用 wartremover Compile

  1. 使用自动插件(需要 sbt 0.13.5+)注入wartremoverSettings,然后手动从Compile.
  2. 禁用自动插件,然后手动将疣去除剂添加到libraryDependencies.

这是第一个选项。

项目/build.properties

sbt.version=0.13.7

项目/wart.sbt

addSbtPlugin("org.brianmckenna" % "sbt-wartremover" % "0.11")

构建.sbt

lazy val Lint = config("lint") extend Compile

lazy val foo = project.
  configs(Lint).
  settings(inConfig(Lint) {
    Defaults.compileSettings ++ wartremover.wartremoverSettings ++
    Seq(
      sources in Lint := {
        val old = (sources in Lint).value
        old ++ (sources in Compile).value 
      },
      wartremover.wartremoverErrors := wartremover.Warts.all
    ) }: _*).
  settings(
    scalacOptions in Compile := (scalacOptions in Compile).value filterNot { _ contains "wartremover" }
  )

foo/src/main/scala/Foo.scala

package foo

object Foo extends App {
  // Won't compile: Inferred type containing Any
  val any = List(1, true, "three")
  println("hi")
}

样本输出

foo> clean
[success] Total time: 0 s, completed Dec 23, 2014 9:43:30 PM
foo> compile
[info] Updating {file:/quick-test/wart/}foo...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /quick-test/wart/foo/target/scala-2.10/classes...
[success] Total time: 1 s, completed Dec 23, 2014 9:43:33 PM
foo> run
[info] Running foo.Foo 
hi
[success] Total time: 0 s, completed Dec 23, 2014 9:43:37 PM
foo> lint:compile
[info] Compiling 1 Scala source to /quick-test/wart/foo/target/scala-2.10/lint-classes...
[error] /quick-test/wart/foo/src/main/scala/Foo.scala:5: Inferred type containing Any
[error]   val any = List(1, true, "three")
[error]       ^
[error] /quick-test/wart/foo/src/main/scala/Foo.scala:5: Inferred type containing Any
[error]   val any = List(1, true, "three")
[error]             ^
于 2014-12-24T02:45:02.320 回答