Description
The user guide suggests that to run scalastyle as part of another task you need to add:
lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
// scalastyle >= 0.9.0
compileScalastyle := scalastyle.in(Compile).toTask("").value
// scalastyle <= 0.8.0
compileScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Compile).toTask("").value
(compile in Compile) := ((compile in Compile) dependsOn compileScalastyle).value
But note that this makes the compile task dependent on the scalastyle task - the scalastyle task executes first. This can cause problems because if the code doesn’t compile, you’ll get the scalastyle errors messages, not the scalac ones. Great though scalastyle is, it can’t match the error messages produced by the compiler. :-)
There is actually another way which doesn't loose the compiler errors:
compile := compileScalastlyeTask(Compile / compile, Compile / scalastyle).value
def compileScalastlyeTask(compileTask: TaskKey[CompileAnalysis],
scalastyleTask: InputKey[Unit]): Def.Initialize[Task[CompileAnalysis]] = Def.task {
val analysis = compileTask.value
val args: Seq[String] = Seq.empty
val scalastyleSourcesV = scalastyleSources.value
val configV = scalastyleConfig.value
val configUrlV = scalastyleConfigUrl.value
val streamsV = streams.value
val failOnErrorV = scalastyleFailOnError.value
val failOnWarningV = scalastyleFailOnWarning.value
val scalastyleTargetV = scalastyleTarget.value
val configRefreshHoursV = scalastyleConfigRefreshHours.value
val targetV = target.value
val configCacheFileV = scalastyleConfigUrlCacheFile.value
org.scalastyle.sbt.Tasks.doScalastyle(args,
configV,
configUrlV,
failOnErrorV,
failOnWarningV,
scalastyleSourcesV,
scalastyleTargetV,
streamsV,
configRefreshHoursV,
targetV,
configCacheFileV)
analysis
}
The idea here is really simple which is to first call the compile task, then the scalastlye task and finally return the result of the compile task. This is quite verbose and not future proof since it is essentially reimplementing the scalastyle
input task.
It would be far simpler if there was a normal task (not an input task) provided by the plugin.