如何创建单独的sbt配置或任务以使用WartRemover进行编译?

Lei*_*and 8 scala sbt scalac

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

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

将Wartremover添加到我之后,我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))
Run Code Online (Sandbox Code Playgroud)

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

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

Eug*_*ota 10

我觉得你真的很亲密.以下是一些细节:

  1. sbt使用Compile配置update任务使用libraryDependencies设置下载库依赖项和编译器插件.addCompilerPlugin为简写libraryDependenciesCompilerPlugin配置.
  2. 编译器插件需要scalaOptions您感兴趣的配置.
  3. 你需要抓住sourcesCompile在使用它们Lint.

如果你看到的执行wartremoverSettings它在做什么都addCompilerPluginscalacOptions.您有两个选项可以禁用wartremover Compile:

  1. 使用自动插件(需要sbt 0.13.5+)进行注入wartremoverSettings,然后手动删除wartremover编译器插件Compile.
  2. 禁用自动插件,然后手动添加疣去除器libraryDependencies.

这是第一个选择.

项目/ build.properties

sbt.version=0.13.7
Run Code Online (Sandbox Code Playgroud)

项目/ wart.sbt

addSbtPlugin("org.brianmckenna" % "sbt-wartremover" % "0.11")
Run Code Online (Sandbox Code Playgroud)

build.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" }
  )
Run Code Online (Sandbox Code Playgroud)

富/ SRC /主/阶/ Foo.scala

package foo

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

样本输出

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]             ^
Run Code Online (Sandbox Code Playgroud)