从 scalastyle sbt 插件中排除文件夹

excluding folder from scalastyle sbt plugin

我发现的关于这个主题的所有其他问题都已经很老了。

我正在使用 sbtscala-style 插件构建一个 scala 项目,但是我找不到一种方法来排除我有一些生成代码的特定文件夹。

有没有办法强制插件不检查特定文件夹?

现在我正在手动编辑文件并添加:

// scalastyle:off

在文件的顶部,但这很烦人。

在官方网站http://www.scalastyle.org/sbt.html我找不到任何文档,虽然看起来实际上可以从中排除paths/files。

https://github.com/scalastyle/scalastyle/blob/e19b54eacb6502b47b1f84d7b2a6b5d33f3993bc/src/main/scala/org/scalastyle/Main.scala#L51

看来我们真的可以通过了:

println(" -x, --excludedFiles STRING      regular expressions to exclude file paths (delimited by semicolons)")

在我的 build.sbt 中,我调用:

lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
compileScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Compile).toTask("").value
(compile in Compile) <<= (compile in Compile) dependsOn compileScalastyle

有没有办法通过 sbt plugin 实现?

您可以获取 "src/main/scala" 下的所有 files/directories 并过滤掉您的目录:

lazy val root = (project in file(".")).
  settings(
    (scalastyleSources in Compile) := {
      // all .scala files in "src/main/scala"
      val scalaSourceFiles = ((scalaSource in Compile).value ** "*.scala").get    
      val fSep = java.io.File.separator // "/" or "\"
      val dirNameToExclude = "com" + fSep + "folder_to_exclude" // "com/folder_to_exclude"
      scalaSourceFiles.filterNot(_.getAbsolutePath.contains(dirNameToExclude))
    }
  )

编辑:
我添加了一个 more "generic" 解决方案,它检查要排除的每个文件的路径...