关于Scala:在sbt AutoPlugin中设置第三方插件设置

Setting a third-party plugin setting in sbt AutoPlugin

我有一个AutoPlugin,它聚集了几个第三方插件,并为我们公司自定义了它们的设置。对于大多数插件,将它们放在projectSettings

中可以很好地工作

1
override lazy val projectSettings = Seq( somePluginSetting :="whatever" )

我也尝试对ScalaStyle进行此操作:

1
2
3
4
5
import org.scalastyle.sbt.ScalastylePlugin.scalastyleConfigUrl

override lazy val projectSettings = Seq(
  scalastyleConfigUrl := Some(url("http://git.repo/scalastyle-config.xml"))
)

此设置在使用我的插件的项目中从不可见,而是sbt使用插件提供的默认值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> inspect scalastyleConfigUrl
[info] Setting: scala.Option[java.net.URL] = None
[info] Description:
[info]  Scalastyle configuration file as a URL
[info] Provided by:
[info]  {file:/Users/kaeser/Documents/workspace/ci-test-project/}root/*:scalastyleConfigUrl
[info] Defined at:
[info]  (org.scalastyle.sbt.ScalastylePlugin) Plugin.scala:101
[info] Delegates:
[info]  *:scalastyleConfigUrl
[info]  {.}/*:scalastyleConfigUrl
[info]  */
*:scalastyleConfigUrl
[info] Related:
[info]  test:scalastyleConfigUrl

当我将设置直接放入build.sbt时,它可以按预期工作。

我制作了一个简单的示例sbt插件来显示问题:https://github.com/jastice/sbt-customsettings

问题可能是什么?


此问题很可能是由应用设置的顺序引起的。如果您的插件和您所依赖的插件都是自动插件,则requires值确定包含项目设置的顺序。

Scalastyle仍使用旧的插件格式。它也没有遵循最佳实践。它不应设置projectSettings,因为这使得在多项目构建中很难禁用它。而且,它还阻止您从自定义插件轻松扩展它。我不确定是否定义了应用项目设置的顺序,或者是由插件的加载顺序决定的。

最简单的解决方法是将Scalastyle设置为AutoPlugin,并使用requires值依赖它。否则,找出决定订单的原因可能很棘手。