How to use an object / type from one subproject to another in a multi-project assembly in SBT?

I have the following folder structure of several projects:

/ComponentA/ComponentA.scala
/ComponentA/build.sbt
/ComponentB/ComponentB.scala
/ComponentB/build.sbt
/project/Build.scala
main.scala

The main.scalafollowing should happen in the root object : ComponentAreturns a String message that ComponentBreads and prints.

Here are the contents of the files:

Componenta STRONG>

object ComponentA {
  def main(args: Array[String]) {
    var myMessage : String = "this message should be passed to ComponentB";
    println("Message to forward: %s \n\n\n ".format(myMessage))
    return myMessage;
  }
}

Componentb

object ComponentB {
  def main(args: Array[String]) {
    println("\n\n\n Inside ComponentB! \n\n\n ")
    println("Message received: %s \n\n\n ".format(args(0)))
  }
}

Build.scala

import sbt._
import Keys._

object RootBuild extends Build {
  lazy val root = Project(id = "root", base = file("."))
    .dependsOn(ComponentA, ComponentB)

  lazy val ComponentA = Project(id = "ComponentA", base = file("ComponentA"))

  lazy val ComponentB = Project(id = "ComponentB", base = file("ComponentB"))
    .dependsOn(ComponentA)

}

main.scala

object ComponentB {
  def main(args: Array[String]) {

    println("\n\n\n Inside main! \n\n\n ")

    // THIS SHOULD HAPPEN:
    // ComponentB(ComponentA());

  }
}

Is this possible with this project structure? If so, what will be the code for main.scala?

+3
source share
1 answer

The next build.sbtin the root project should allow you to create this project structure.

lazy val ComponentA = project

lazy val ComponentB = project dependsOn ComponentA

lazy val root = project in file(".") dependsOn (ComponentA, ComponentB) aggregate (ComponentA, ComponentB)

You will need to fix a few problems in the component objects so that they compile, but the path to the project classes should be good.

, , root dependsOn ComponentAB, dependsOn ComponentB (, , ComponentA ComponentB ).

.

Componenta/ComponentA.scala

object ComponentA {
  def apply(): String = {
    var myMessage = "this message should be passed to ComponentB"
    println(s"Message to forward: $myMessage\n\n\n")
    myMessage
  }
}

ComponentB/ComponentB.scala

object ComponentB {
  def apply(msg: String) = {
    println("\n\n\n Inside ComponentB! \n\n\n ")
    println("Message received: $msg\n\n\n")
  }
}

Main.scala

object MainObject {
  def main(args: Array[String]) {
    println("\n\n\n Inside main! \n\n\n ")
    //ComponentB(ComponentA())
    ComponentA()
  }
}

, run, :

[root]> run
[info] Running ComponentB



 Inside main!



Message to forward: this message should be passed to ComponentB



[success] Total time: 0 s, completed Jan 20, 2014 9:59:32 PM
0

All Articles