如何在build.sbt中指定驻留在另一个模块中的mainClass?

时间:2017-04-20 21:48:19

标签: scala build sbt

出于某种原因,我们的项目重组了另一个模块中抛出的主类

我在build.sbt中指定了mainClass如下所示,但仍然找不到类错误:

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

但是,这肯定会失败,因为它会在src文件夹中查找Main类。但是,此模块位于({1}}的兄弟之外:

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并使用此结构找到主类?

2 个答案:

答案 0 :(得分:0)

它应该有用。

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).

答案 1 :(得分:0)

假设您有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