Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a custom SBT Setting in sub-projects

Tags:

scala

sbt

Somewhat similar to this question, how can reference a custom setting in a sub project.

In 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"))

In thrift/build.sbt:

name := "thrift"

// doesn't work
libraryDependencies ++= Seq(
  "com.twitter" %% "finagle-thriftmux" % (finagleVersion in LocalRootProject).value
)
like image 764
reikje Avatar asked Oct 02 '14 07:10

reikje


People also ask

How do we specify library dependencies in sbt?

The libraryDependencies key Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.

Where are sbt dependencies?

If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.


1 Answers

.sbt files cannot see the definitions (e.g., vals) in other .sbt files, even if they are part of the same build.

However, all .sbt files in a build can see/import the content of project/*.scala files. So you'll have to declare your val finagleVersion in a .scala file:

project/CustomKeys.scala:

import sbt._
import Keys._

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

Now, in your .sbt files, just

import CustomKeys._

and you're good to go.

like image 74
sjrd Avatar answered Nov 11 '22 03:11

sjrd