scalasbtsubproject

Sbt nested dependsOn


I have a root project that depends on a subproject1. And subproject1 depends on subproject2. Does that imply that I Can use subproject2's source code directly in root?

lazy val root =
  Project(id = "root", base = file(".")).dependsOn(sub1)

lazy val sub1 =
  Project(id = "sub1").dependsOn(sub2)

lazy val sub2 =
  Project(id = "sub2")

Solution

  • Yes.

    This can easily be checked.

    build.sbt

    name := "sbtdemo"
    
    version := "0.1"
    
    ThisBuild / scalaVersion := "2.13.4"
    
    lazy val root =
      Project(id = "root", base = file(".")).dependsOn(sub1)
    
    lazy val sub1 =
      Project(id = "sub1", base = file("sub1")).dependsOn(sub2)
    
    lazy val sub2 =
      Project(id = "sub2", base = file("sub2"))
    

    sub2/src/main/scala/App.scala

    object App {
      def foo() = println("foo")
    }
    

    src/main/scala/Main.scala

    object Main {
      def main(args: Array[String]): Unit = {
        App.foo() // foo
      }
    }