问题描述
我有以下多项目文件夹结构:
I have the following multi-project folder structure:
/ComponentA/ComponentA.scala
/ComponentA/build.sbt
/ComponentB/ComponentB.scala
/ComponentB/build.sbt
/project/Build.scala
main.scala
在根对象 main.scala
中应该发生以下情况:ComponentA
返回一个 String 消息,ComponentB
读入并打印出来.
In the root object main.scala
the following should happen: ComponentA
returns a String message that ComponentB
reads in and prints out.
文件内容如下:
组件A
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;
}
}
组件B
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
的代码将如何?
Is this possible with that project structure? If so, how will the code be for main.scala
?
推荐答案
根项目中的以下 build.sbt
应该让您创建该项目结构.
The following build.sbt
in the root project should let you create that 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'll have to fix a few issues in the component objects so they compile, but the project's classpath should be fine.
然而,将根项目作为子模块的聚合是一种常见的方法,因此在您的情况下,root
不应将 dependsOn
与另一个单独的项目 ComponentAB
将 dependsOn ComponentB
(因此 ComponentA
作为 ComponentB
已经依赖于它).
It is however a common approach to have a root project an aggregate for submodules so in your case root
should not have dependsOn
with another separate project ComponentAB
that would dependsOn ComponentB
(and hence ComponentA
as ComponentB
already depends on it).
下面的代码应该非常小心,并且只是为了问题.
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
时,您应该得到以下输出:
With the files, when you do run
you should get the following output:
[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
这篇关于如何在 SBT 的多项目构建中使用另一个子项目中的对象/类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!