简体   繁体   English

如何在SBT中的多项目构建中使用另一个子项目中的对象/类型?

[英]How to use object/type from one subproject in another in multi-project build in SBT?

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

In the root object main.scala the following should happen: ComponentA returns a String message that ComponentB reads in and prints out. 在根对象main.scala ,应该发生以下情况: ComponentA返回ComponentB读入并打印出来的String消息。

Here are the contents of the files: 以下是文件的内容:

ComponentA 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 以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 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 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 that project structure? 这可能与项目结构有关吗? If so, how will the code be for main.scala ? 如果是这样,代码将如何用于main.scala

The following build.sbt in the root project should let you create that project structure. 根项目中的以下build.sbt应该允许您创建该项目结构。

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. 您必须在组件对象中修复一些问题,以便进行编译,但项目的类路径应该没问题。

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). 然而,它是一种常见的方法有根项目子模块的集合所以你的情况root不应该有dependsOn与另外一个项目ComponentAB那会dependsOn ComponentB (因此ComponentA作为ComponentB已经依赖于它)。

The code below should be taken with a great care and only for the sake of the question. 下面的代码应该非常谨慎,只是为了问题。

ComponentA/ComponentA.scala 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 以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 Main.scala

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

With the files, when you do run you should get the following output: 使用这些文件,当您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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM