3

SBT scalariform 插件在多项目设置中多次格式化文件。这是示例https://github.com/Seetaramayya/sbt-scalariform-example

如果您编译项目,您可以在控制台中看到类似的内容

[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "root")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module1")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module2")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "main")(compile) ...

代码库中有 5 个 scala 文件和 2 个 sbt 文件。在 SBT 和 scalariform 文档中,我都找不到如何在多模块设置中只执行一次任务。

我只想执行一次任务。disablePlugins我看到的唯一选项是在根项目级别启用插件并在子项目中禁用,但我不喜欢该选项(我需要添加15子项目)

4

1 回答 1

1

问题似乎是

每个子项目scalariformFormat在其不同范围内的任务在其sourceDirectories设置中都有项目根。这只会导致整个项目被爬取以获取 scala 源并格式化与子项目一样多的次数。

并已通过设置解决scalariformWithBaseDirectory

val scalariformWithBaseDirectory = settingKey[Boolean]("Whether or not to format sources in project root (default: false)")

因此设置withBaseDirectory=false应该.scalariform.conf给出输出

[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...

但是请注意,这不会格式化*.scalaproject/目录中的源。为了解决这个问题,我们可以尝试保留withBaseDirectory=false.conf像这样为根项目打开它

lazy val root = (project in file("."))
  .aggregate(module1, module2, main)
  .settings(scalariformWithBaseDirectory := true)

哪个输出

[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "root")(compile) ...

我们仍然会得到重复,但不像最初那样多。

于 2019-04-13T23:14:41.900 回答