如何在sbt构建中跳过Scalastyle?单次构建禁用的命令行方法
Hey there! Let's break down answers to your two Scalastyle + sbt questions, tailored to how you've integrated Scalastyle with your compile task.
1. Skip Scalastyle Checks Permanently in Build Configuration
Since you've wired compileScalastyle to your compile task, you have a couple of straightforward options to disable it long-term:
- Remove the task dependency: If your build has a line like
compile in Compile := (compile in Compile).dependsOn(compileScalastyle).value, simply comment or delete that line. This breaks the link between compilation and Scalastyle checks entirely. - Add a configurable toggle: Define a setting key to control whether Scalastyle runs, so you can flip it on/off without rewriting task logic. Update your build.sbt like this:
To permanently skip checks, just setlazy val skipScalastyle = settingKey[Boolean]("Toggle to skip Scalastyle checks") skipScalastyle := false // Default to running checks lazy val compileScalastyle = taskKey[Unit]("compileScalastyle") compileScalastyle := { if (!skipScalastyle.value) { scalastyle.in(Compile).toTask("").value } } // Keep your existing compile task dependency compile in Compile := (compile in Compile).dependsOn(compileScalastyle).valueskipScalastyle := truein your build.
2. Disable Scalastyle Temporarily via Command Line (Like Maven's -Dcheckstyle.skip=true)
Scalastyle's sbt plugin doesn't include a built-in system property for this, but we can easily add one ourselves to match the Maven-style workflow:
Option 1: Use a System Property Toggle
Update the skipScalastyle setting from the previous section to read a system property:
lazy val skipScalastyle = settingKey[Boolean]("Toggle to skip Scalastyle checks") skipScalastyle := sys.props.get("scalastyle.skip").exists(_.toBoolean)
Now you can run sbt with the following command to skip Scalastyle for just that build:
sbt -Dscalastyle.skip=true compile
Option 2: Override the Task Directly (No Build Changes Needed)
If you don't want to modify your build.sbt at all, you can override the compileScalastyle task directly from the command line to do nothing:
sbt "set compileScalastyle := {}" compile
Or target the underlying scalastyle task directly:
sbt "set scalastyle.in(Compile) := {}" compile
Either approach will let you skip Scalastyle checks for a single build without altering your permanent configuration.
内容的提问来源于stack exchange,提问作者Cedric Reichenbach

