簡體   English   中英

如何在build.sbt中指定駐留在另一個模塊中的mainClass?

[英]How can I specify a mainClass in build.sbt that resides in another module?

由於某種原因,我們的項目進行了重組,將主類放入了另一個模塊中

我在mainClass指定了mainClass ,如下所示,但仍然出現未找到類錯誤:

mainClass in Compile := Some("com.so.questions.sbt.Main")

但是,這肯定會失敗,因為它將在src文件夾中查找Main類。 但是,此模塊位於src (的兄弟)之外:

MyScalaProject
+-MyModule
|+-src
| +-com.so.questions.sbt
|  +-Main
|+-build.sbt <-- build.sbt specific to this module, currently blank
+-src
| +-<other folders>
+-build.sbt  <-- build.sbt currently housing all config

如何更改build.sbt的項目范圍以查找並正確加載主類?

也就是說,是否可以在頂級sbt run並通過這種結構找到主類?

它應該工作。

根據我的理解, mainClass的FQCN規范應獨立於位置。

真正想到的問題是如何加載子模塊。 以下是一些sbt定義,這些定義應有助於您指出正確的方向(將<>標記替換為您自己的項目ID):

// Define a submodule ref to be able to include it as a dependency
lazy val subModuleRef = ProjectRef(file("MyModule"),<MyModule SBT NAME>)

// Define a submodule project to be able to orchestrate it's build 
lazy val subModule = Project(
  id = <MyModule SBT NAME>,
  base = file("MyModule"),
).addSbtFiles(file("build.sbt"))

// Define the top-level project, depending and subModule Ref for code
// inclusion and aggregating the subModule for build orchestration
lazy val scalaProject = Project(
  id = <MyScalaProject NAME>,
  base = file("."),
  aggregate = Seq(subModule),
  settings = commonSettings
).dependsOn(subModuleRef).

假設您有MyModule模塊/文件夾,其中包含主類和其他名為MyCoreModule模塊(僅用於說明整個build.sbt):

// any stuff that you want to share between modules
lazy val commonSettings = Seq(
    scalaVersion  := "2.12.8",
    version       := "1.0-SNAPSHOT"
)

lazy val root = (project in file("."))
  .settings(commonSettings: _*)
  .settings(
    name := "parent-module"
  )
  .aggregate(core, app)
  .dependsOn(app) // <-- here is the config that will allow you to run "sbt run" from the root project

lazy val core = project.in(file("MyCoreModule"))
  .settings(commonSettings: _*)
  .settings(
    name := "core"
  )

lazy val app = project.in(file("MyModule"))
  .dependsOn(core)
    .settings(commonSettings: _*)
  .settings(
    name := "app"
    )

// define your mainClass from the "app" module
mainClass in Compile := (mainClass in Compile in app).value

順便說一句, sbt.version=1.2.7

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM