如何在 SBT 多项目中共享自定义任务

How do you share a custom task in a SBT multi-project

我有一个项目设置为 SBT 多构建。看起来像这样:

- project
   Dependencies.scala
- core
   build.sbt
- server
   build.sbt
build.sbt

我想使用 Dependencies.scala 作为子项目之间共享的库版本号的容器。

sealed trait Dependencies {
  val commonsIo = "2.4"
}

object DependencyVersions extends Dependencies

在根 build.sbt 我添加了一个 Setting 给每个子项目。

lazy val dependencies = settingKey[Dependencies]("versions")

val defaultSettings = Defaults.coreDefaultSettings ++ Seq(
    dependencies := DependencyVersions)

def projectFolder(name: String, theSettings: Seq[Def.Setting[_]] = Nil) = Project(name, file(name), settings = theSettings)

lazy val core = projectFolder("core", defaultSettings)

我无法访问 core/build.sbt 中的 dependencies 设置。

"commons-io" % "commons-io" % dependencies.value.commonsIo, <-- doesn't work

我怎样才能让它工作?

您可以在 object Common extends AutoPlugin(在 project/Common.scala)中定义通用设置(依赖项),然后在子项目描述符上使用 .enablePlugin(Common)(参见 Anorm).

感谢@cchantep 使用下面的 AutoPlugin 现在可以正常工作了

import sbt._

sealed trait Dependencies {
  val commonsIo = "2.4"
}

object DependencyVersions extends Dependencies

object DependencyVersionsPlugin extends AutoPlugin {
  override def trigger = allRequirements

  object autoImport {
    lazy val dependencies = settingKey[Dependencies]("Bundles dependency versions")
  }

  import autoImport._

  override def projectSettings = Seq(
    dependencies := DependencyVersions
  )
}