更改当前sbt任务范围中的变量

Rea*_*onk 7 scala sbt

这会编译,但不会添加scalacOptionscompile任务中.这样做的正确方法是什么?

compileWall in ThisBuild := Def.task {
  scalacOptions += "-Xfatal-warnings"
  (compile in Compile).value
}.value
Run Code Online (Sandbox Code Playgroud)

che*_*ohi 3

SBT设置在运行时是不可变的,因此我们无法scalacOptions在自定义中更新Task

http://www.scala-sbt.org/0.13/docs/Full-Def.html#Reminder%3A+it%E2%80%99s+all+immutable

但是有一种方法可以通过创建自定义配置并绑定此配置scalacOptions来实现自定义更改,例如:TaskscalacOptions

lazy val MyCompile = config("MyCompile") extend Compile // customize config: MyCompile
configs(MyCompile) //configs 
inConfig(MyCompile)(Defaults.configTasks) //inConfig and append the Defaults.configTasks

val compileWall = taskKey[Unit]("compileWall")

compileWall in ThisBuild := {
  (compile in MyCompile).value
}

scalacOptions in MyCompile := Seq("-Xfatal-warnings") // bind the scalacOptions in customize config.
Run Code Online (Sandbox Code Playgroud)