SBT scalariform插件多次格式化同一件事,有可能一次吗?

时间:2019-04-13 13:30:34

标签: scala sbt scalariform

SBT scalariform plugin在多项目设置中多次格式化文件。这是示例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中还是在标量文档中,我都找不到如何在多模块设置中仅执行一次任务。

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

1 个答案:

答案 0 :(得分:1)

issue似乎就是那个

  

每个子项目在不同范围内的scalariformFormat任务   将项目根目录设置为sourceDirectories。这只是   导致对整个项目进行爬网以获取scala源,并且   格式化次数与子项目一样多。

并且已通过addressed设置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) ...

但是请注意,这不会格式化*.scala根目录中的project/源。为了解决这个问题,我们可以尝试将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) ...

我们仍然会得到重复,但不如最初。