使用 sbt-projectmatrix 创建 AutoPlugin。如何设置scalaVersion?

Use sbt-projectmatrix to create AutoPlugin. How to set scalaVersion?

我使用 sbt-projectmatrix 创建 2 个带有 customRow 的 sbt 插件:

case class MatrixAxis(
  idSuffix: String,
  directorySuffix: String
) extends VirtualAxis.WeakAxis

// sbt
val oss = MatrixAxis("-oss", "oss")
val priv = MatrixAxis("-priv", "priv")
val scalaV = ???
lazy val devops = projectMatrix
  .enablePlugins(SbtPlugin)
  .settings(
    pluginCrossBuild / sbtVersion := "1.3.13", // minimum sbt version
  ).customRow(
    scalaVersions = Seq(scalaV),
    axisValues = Seq(priv, VirtualAxis.jvm),
    _.settings(...)
  .customRow(
    scalaVersions = Seq(scalaV),
    axisValues = Seq(oss, VirtualAxis.jvm),
    _.settings(...)

sbt's guide中,他们说:

sbt plugins must be compiled with Scala 2.12.x that sbt itself is compiled in. By NOT specifying scalaVersion, sbt will default to the Scala version suited for a plugin.

但我不知道如何在不设置 scalaVersions 的情况下创建自定义行?

(如果我设置 scalaVersions = Nil 那么 projectMatrix 将不会为我创建任何项目)

sbt-projectmatrix 需要静态知道 Scala 版本,因为除其他外,它使用它来构建项目的 ID。所以不幸的是,在 scalaVersions 参数中,我们无法读取 scalaVersion.value 的值来知道默认情况下 sbt 会选择什么。

我们可以做的是稍微作弊:首先,使用任何硬编码的2.12.x版本(例如2.12.1)写入scalaVersions。然后,显式覆盖 scalaVersion := ...,绕过 sbt-projectmatrix 将设置的 2.12.1:

val oss = MatrixAxis("-oss", "oss")
val priv = MatrixAxis("-priv", "priv")
val scalaV = "2.12.1" // arbitrary 2.12.x version
lazy val devops = projectMatrix
  .enablePlugins(SbtPlugin)
  .settings(
    pluginCrossBuild / sbtVersion := "1.3.13", // minimum sbt version
    /* override with the default scalaVersion in the Global scope,
     * which is sbt's choice, not impacted by sbt-projectmatrix
     */
    scalaVersion := (Global / scalaVersion).value,
  ).customRow(
    scalaVersions = Seq(scalaV),
    axisValues = Seq(priv, VirtualAxis.jvm),
    _.settings()
  )
  .customRow(
    scalaVersions = Seq(scalaV),
    axisValues = Seq(oss, VirtualAxis.jvm),
    _.settings()
  )