Home > Back-end >  Use sbt-projectmatrix to create AutoPlugin. How to set scalaVersion?
Use sbt-projectmatrix to create AutoPlugin. How to set scalaVersion?

Time:09-29

I use sbt-projectmatrix to create 2 sbt plugins with customRow:

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(...)

In sbt's guide, they said:

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.

But I don't know how to create a customRow without setting scalaVersions?

(If I set scalaVersions = Nil then projectMatrix will not create any project for me)

CodePudding user response:

sbt-projectmatrix requires to know the Scala version statically, because, among other things, it uses it to build the ID of the project. So unfortunately, in the scalaVersions argument, we cannot read the value of scalaVersion.value to know what sbt would pick by default.

What we can do is to cheat a little: first, use any hard-coded 2.12.x version (e.g., 2.12.1) to write in scalaVersions. Then, explicitly override scalaVersion := ..., by-passing the 2.12.1 that sbt-projectmatrix would set:

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()
  )
  • Related