如何在子项目中引用自定义SBT设置

时间:2014-10-02 07:39:36

标签: scala sbt

与此question有些相似,如何在子项目中引用自定义设置。

在build.sbt:

import sbt.Keys._

val finagleVersion = settingKey[String]("Defines the Finagle version")

val defaultSettings = Defaults.coreDefaultSettings ++ Seq(
  finagleVersion in ThisBuild := "6.20.0",
  organization in ThisBuild := "my.package",      
  scalaVersion in ThisBuild := "2.10.4",
  version in ThisBuild := "0.1-SNAPSHOT"
)

lazy val root = project.in(file(".")).aggregate(thrift).settings(
  publishArtifact in (Compile, packageBin) := false,
  publishArtifact in (Compile, packageDoc) := false,
  publishArtifact in (Compile, packageSrc) := false
)

lazy val thrift = project.in(file("thrift"))

在thrift / build.sbt中:

name := "thrift"

// doesn't work
libraryDependencies ++= Seq(
  "com.twitter" %% "finagle-thriftmux" % (finagleVersion in LocalRootProject).value
)

1 个答案:

答案 0 :(得分:9)

.sbt个文件无法查看其他val文件中的定义(例如.sbt s),即使它们属于同一版本。

但是,构建中的所有.sbt个文件都可以查看/导入project/*.scala个文件的内容。因此,您必须在val finagleVersion文件中声明.scala

project/CustomKeys.scala

import sbt._
import Keys._

object CustomKeys {
  val finagleVersion = settingKey[String]("Defines the Finagle version")
}

现在,在您的.sbt文件中,只需

import CustomKeys._

你很高兴。