如何动态加载 SBT 插件

dav*_*rez 2 sbt

sbt -plugin不适用于git worktrees

所以我想有条件地加载这个 SBT 插件。

dav*_*rez 5

代替

addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")
Run Code Online (Sandbox Code Playgroud)

添加这些行plugins.sbt

libraryDependencies ++= {
    if (baseDirectory.value / "../.git" isDirectory)
        Seq(Defaults.sbtPluginExtra("com.typesafe.sbt" % "sbt-git" % "0.8.5", (sbtBinaryVersion in update).value, (scalaBinaryVersion in update).value))
    else {
        println("sbt-git plugin not loaded")
        Seq.empty
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 SBT 0.13.9 进行测试。

更新

它也适用于 SBT 1.7.1,但语法略有不同:

libraryDependencies ++= {
    if (baseDirectory.value / "../.git" isDirectory)
        Seq(Defaults.sbtPluginExtra("com.typesafe.sbt" % "sbt-git" % "0.8.5", (update/sbtBinaryVersion).value, (update/scalaBinaryVersion).value))
    else {
        println("sbt-git plugin not loaded")
        Seq.empty
    }
}
Run Code Online (Sandbox Code Playgroud)