繁体   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