这是一个 .sbt 构建定义 的示例,展示了如何使用配置来对依赖项进行分组。
utils
模块为其他模块提供实用程序。它使用配置对依赖项进行分组,以便依赖项目无需在只使用部分功能的情况下引入所有依赖项。这可以作为拥有多个实用程序模块(以及多个实用程序 jar 包)的替代方案。
在这个示例中,考虑一个 utils
项目,它提供了与 Scalate 和 Saxon 相关的实用程序。因此,它需要在编译类路径中使用 Scalate 和 Saxon,而使用 utils
的所有功能的项目也需要这些依赖项。但是,项目 a
只需要与 Scalate 相关的实用程序,因此不需要 Saxon。通过仅依赖 utils
的 scalate
配置,它只获得了与 Scalate 相关的依赖项。
// Custom configurations
lazy val Common = config("common").describedAs("Dependencies required in all configurations.")
lazy val Scalate = config("scalate").extend(Common).describedAs("Dependencies for using Scalate utilities.")
lazy val Saxon = config("saxon").extend(Common).describedAs("Dependencies for using Saxon utilities.")
// Define a customized compile configuration that includes
// dependencies defined in our other custom configurations
lazy val CustomCompile = config("compile").extend(Saxon, Common, Scalate)
// factor out common settings
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.12.18"
ThisBuild / version := "0.1.0-SNAPSHOT"
// An example project that only uses the Scalate utilities.
lazy val a = (project in file("a"))
.dependsOn(utils % "compile->scalate")
// An example project that uses the Scalate and Saxon utilities.
// For the configurations defined here, this is equivalent to doing dependsOn(utils),
// but if there were more configurations, it would select only the Scalate and Saxon
// dependencies.
lazy val b = (project in file("b"))
.dependsOn(utils % "compile->scalate,saxon")
// Defines the utilities project
lazy val utils = (project in file("utils"))
.settings(
inConfig(Common)(Defaults.configSettings), // Add the src/common/scala/ compilation configuration.
addArtifact(Common / packageBin / artifact, Common / packageBin), // Publish the common artifact
// We want our Common sources to have access to all of the dependencies on the classpaths
// for compile and test, but when depended on, it should only require dependencies in 'common'
Common / classpathConfiguration := CustomCompile,
// Modify the default Ivy configurations.
// 'overrideConfigs' ensures that Compile is replaced by CustomCompile
ivyConfigurations := overrideConfigs(Scalate, Saxon, Common, CustomCompile)(ivyConfigurations.value),
// Put all dependencies without an explicit configuration into Common (optional)
defaultConfiguration := Some(Common),
// Declare dependencies in the appropriate configurations
libraryDependencies ++= Seq(
"org.fusesource.scalate" % "scalate-core" % "1.5.0" % Scalate,
"org.squeryl" %% "squeryl" % "0.9.5-6" % Scalate,
"net.sf.saxon" % "saxon" % "8.7" % Saxon
)
)