WartRemover是一个scalac插件。通常,它是通过sbt plugin配置的。
我希望能够在我的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))
之后scalacOptions
大致包含了我在新lint
配置和compile
配置中的预期。但是,当我在调试模式下使用sbt运行lint:compile
和compile
时,我可以看到scalac的命令行参数,要么两个命令都会通过-P:wartremover:...
开关。这是令人惊讶的,因为只有lint:scalacOptions
显示-P:wartremover:...
开关。
如何在不影响compile:compile
的情况下创建单独的sbt配置或任务以使用WartRemover进行编译?
答案 0 :(得分:10)
我觉得你非常接近。以下是一些细节:
Compile
配置的update
任务下载库依赖项和编译器插件,该任务使用libraryDependencies
设置。 addCompilerPlugin
是libraryDependencies
CompilerPlugin
配置的简写。scalaOptions
。sources
抓取Compile
以在Lint
中使用它们。如果您看到wartremoverSettings
的实施,它同时执行addCompilerPlugin
和scalacOptions
。您有两种方法可以在Compile
上禁用wartremover:
wartremoverSettings
,然后从Compile
手动删除wartremover编译器插件。libraryDependencies
。这是第一个选项。
sbt.version=0.13.7
addSbtPlugin("org.brianmckenna" % "sbt-wartremover" % "0.11")
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" }
)
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] ^