これは、構成を使用して依存関係をグループ化する方法を示す .sbt ビルド定義の例です。
utils
モジュールは、他のモジュールにユーティリティを提供します。依存プロジェクトが機能の一部のみを使用する場合にすべての依存関係をプルインする必要がないように、構成を使用して依存関係をグループ化します。これは、複数のユーティリティモジュール(したがって、複数のユーティリティjar)を持つことの代替手段になり得ます。
この例では、Scalate と Saxon の両方に関連するユーティリティを提供する utils
プロジェクトを考えてみましょう。したがって、コンパイルクラスパスに 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
)
)