基于 scalaVersion 的条件 addSbtPlugin

Conditional addSbtPlugin based on scalaVersion

我正在使用仅适用于 Scala 2.11 的插件 (sbt-scapegoat)。

我可以有一个基于 scalaVersion 的有条件的 addSbtPlugin 吗?喜欢:

if (scalaVersion.value.startsWith("2.11")) addSbtPlugin("com.sksamuel.scapegoat" %% "sbt-scapegoat" % "0.94.6")

如何在 SBT 中执行此操作?

剑石

tl;dr根据问题描述不可能。

sbt 项目中涉及至少 两种构建配置 - 真实项目(您想赌一把)和用于构建项目的元构建.是的,我知道这听起来有点奇怪,但恕我直言,这是一个非常强大的概念。

参见sbt is recursive

The project directory is another build inside your build, which knows how to build your build. To distinguish the builds, we sometimes use the term proper build to refer to your build, and meta-build to refer to the build in project. The projects inside the metabuild can do anything any other project can do. Your build definition is an sbt project.

sbt 在 Scala 之上运行并且需要它的严格版本。没有办法改变它,除非你想花时间在你一开始就不应该接触的事情上:)

你可以做的是在 project/plugins.sbt 中应用插件,然后在项目中,根据 scalaVersion 有选择地应用插件的 设置 ] 项目的构建 而不是 元构建本身。

它并不像答案上写的那么复杂,但解释简单的概念对我来说通常不是一件容易的事。与 sbt 一起玩得开心!只要使用得当,它很快就会还给你的。

2020 年更新后的答案:您可以在 addSbtPlugin 上使用 .filter

例如下面的作品:

val scalafixEnabled = System.getProperty("SCALAFIX", "").trim.nonEmpty

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.14").filter(_ => scalafixEnabled)