我有以下多项目文件夹结构:
/ComponentA/ComponentA.scala
/ComponentA/build.sbt
/ComponentB/ComponentB.scala
/ComponentB/build.sbt
/project/Build.scala
main.scala
在根对象main.scala
中,应发生以下情况:ComponentA
返回ComponentB
读入并打印出来的字符串消息。
以下是文件的内容:
ComponentA
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());
}
}
这可能与项目结构有关吗?如果是这样,代码将如何用于main.scala
?
答案 0 :(得分:0)
根项目中的以下build.sbt
应该可以让您创建该项目结构。
lazy val ComponentA = project
lazy val ComponentB = project dependsOn ComponentA
lazy val root = project in file(".") dependsOn (ComponentA, ComponentB) aggregate (ComponentA, ComponentB)
您必须在组件对象中修复一些问题,以便进行编译,但项目的类路径应该没问题。
然而,将根项目作为子模块的聚合是一种常见的方法,因此在您的情况下,root
不应该dependsOn
与另一个单独的项目ComponentAB
dependsOn ComponentB
ComponentA
(因此ComponentB
object ComponentA {
def apply(): String = {
var myMessage = "this message should be passed to ComponentB"
println(s"Message to forward: $myMessage\n\n\n")
myMessage
}
}
已取决于它。)
下面的代码应该非常谨慎,只是为了提问。
<强> ComponentA / ComponentA.scala 强>
object ComponentB {
def apply(msg: String) = {
println("\n\n\n Inside ComponentB! \n\n\n ")
println("Message received: $msg\n\n\n")
}
}
<强>以componentB / ComponentB.scala 强>
object MainObject {
def main(args: Array[String]) {
println("\n\n\n Inside main! \n\n\n ")
//ComponentB(ComponentA())
ComponentA()
}
}
<强> Main.scala 强>
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
时,您应该得到以下输出:
{{1}}