SBT: How to include sbt files in upper directory to my build.sbt?

Viewed 1082

I have a many dependent sbt projects in one folder. They all have same values in Build.sbt, for example dependencies.

I want to move same values from all sbt files to separate file. But don't want to use multibuild. Just need to include some other sbt files from upper directory.

For example my directory structure can look like this:

MyRepository
  |- Dependencies.sbt
  |- MyProject1
      |- src
      |- Build.sbt
  |- MyProject2
      |- src
      |- Build.sbt

In that example, how can I include Dependencies.sbt in Build.sbt?

1 Answers

Code is reused between .sbt files by creating a normal .scala file in project/. The code in project/ will be available for use in the .sbt files.

If I remember correctly the definitions in one .sbt are not visible to other .sbt files, at least on the older versions.

Basically, the solution is to use: Dependencies.scala and not Dependencies.sbt and define the common part's there.

Check the illustration, that can be found here,

You create project/Dependencies.scala to track dependencies in one place.

    import sbt._

    object Dependencies {
      // Versions
      lazy val akkaVersion = "2.3.8"

      // Libraries
      val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion
      val akkaCluster = "com.typesafe.akka" %% "akka-cluster" % akkaVersion
      val specs2core = "org.specs2" %% "specs2-core" % "2.4.17"

      // Projects
      val backendDeps =
        Seq(akkaActor, specs2core % Test)
    }

The Dependencies object will be available in build.sbt. You need to, import Dependencies._ in your build.sbt file.

    import Dependencies._

    ThisBuild / organization := "com.example"
    ThisBuild / version      := "0.1.0-SNAPSHOT"
    ThisBuild / scalaVersion := "2.12.8"

    lazy val backend = (project in file("backend"))
      .settings(
        name := "backend",
        libraryDependencies ++= backendDeps
      )
Related