问题
I want my scalacSettings
to be more strict (more linting) when I issue my own command validate
.
What is the best way to achieve that?
A new scope (strict
) did work, but it requires to compile the project two times when you issue test
. So that's not a option.
回答1:
SBT custom command allows for temporary modification of build state which can be discarded after command finishes:
def validate: Command = Command.command("validate") { state =>
import Project._
val stateWithStrictScalacSettings =
extract(state).appendWithSession(
Seq(Compile / scalacOptions ++= Seq(
"-Ywarn-unused:imports",
"-Xfatal-warnings",
"...",
))
,state
)
val (s, _) = extract(stateWithStrictScalacSettings).runTask(Test / test, stateWithStrictScalacSettings)
s
}
commands ++= Seq(validate)
or more succinctly using :: convenience method for State
transformations:
commands += Command.command("validate") { state =>
"""set scalacOptions in Compile := Seq("-Ywarn-unused:imports", "-Xfatal-warnings", "...")""" ::
"test" :: state
}
This way we can use sbt test
during development, while our CI hooks into sbt validate
which uses stateWithStrictScalacSettings
.
来源:https://stackoverflow.com/questions/54309976/conditional-scalacsettings-settingkey