[英]How can I specify a mainClass in build.sbt that resides in another module?
For some reason, our project got reorganized with the main class thrown in another module 由于某种原因,我们的项目进行了重组,将主类放入了另一个模块中
I've specified the mainClass
as below in the build.sbt but I still get a class not found error: 我在
mainClass
指定了mainClass
,如下所示,但仍然出现未找到类错误:
mainClass in Compile := Some("com.so.questions.sbt.Main")
However, this is bound to fail since it's going to look for the Main class in the src
folder. 但是,这肯定会失败,因为它将在
src
文件夹中查找Main类。 However, this module lives outside of (sibling of) src
: 但是,此模块位于
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
How can I change the project scope in build.sbt
to find and correctly load the main class? 如何更改
build.sbt
的项目范围以查找并正确加载主类?
That is, is it possible to do sbt run
at the top level and have the main class be found with this structure? 也就是说,是否可以在顶级
sbt run
并通过这种结构找到主类?
It should work. 它应该工作。
The FQCN specification for mainClass
should be location independent to my understanding. 根据我的理解,
mainClass
的FQCN规范应独立于位置。
The real question that comes to mind is how you are loading your sub-module. 真正想到的问题是如何加载子模块。 Here are some sbt definitions that should help point you in the right direction ( replace the
<>
tags with your own project Ids) : 以下是一些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).
Let's say that you have the MyModule
module/folder containing the main class and some other module called MyCoreModule
(just to illustrate the whole build.sbt): 假设您有
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
Btw, sbt.version=1.2.7
顺便说一句,
sbt.version=1.2.7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.