We're combining two of our Scala projects because they share a lot of functionality. I'm setting up the new repo, and have the following build.sbt:
ThisBuild / scalaVersion := "2.13.10"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.15" % Test
lazy val project1 = (project in file("project1"))
.dependsOn(core)
.settings(
name := "Project 1",
assembly / mainClass := Some("project1.Main"),
assembly / assemblyJarName := "project1.jar",
)
lazy val project2 = (project in file("project2"))
.dependsOn(core)
.settings(
name := "Project 2",
assembly / mainClass := Some("project2.Main"),
assembly / assemblyJarName := "project2.jar",
)
lazy val core = (project in file("core"))
.settings(
name := "Core",
)
Basically, Project 1 and Project 2 should both depend on Core, but not on each other.
My src directory looks like this:
src
main/scala
core
Core.scala
project1
Main.scala
project2
Main.scala
test/scala
core
TestCore.scala
project1
TestProject1.scala
project2
TestProject2.scala
For the moment, Core.scala is:
package core
object Core {
def hello() = {
println("hello from core!")
}
}
project1/Main.scala is:
package project1
import core.Core
object Main {
def main(args: Array[String]): Unit = {
hello()
Core.hello()
}
def hello() = { println("hello from project 1!") }
}
and similarly, project2/Main.scala is:
package project2
import core.Core
object Main {
def main(args: Array[String]): Unit = {
hello()
Core.hello()
}
def hello() = { println("hello from project 2!") }
}
I can run both of these main classes in the sbt terminal from the project root, either by entering run and selecting the main class I'd like to use, or entering runMain <main_class> (the two main classes are project1.Main and project2.Main). However, if I switch to one of the subprojects (for example, by entering project project1) and then enter run, sbt is no longer able to detect a main class. Additionally, if I compile the project using assembly, I can't run either project1.jar or project2.jar because they can't find their main classes either. Why exactly is this happening?
Furthermore, I noticed that I can still import project2 into Project 1 and project1 into Project 2, even though I've only set dependsOn for core. Is this expected behavior?