scala sbt:选择运行的主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8612614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sbt: selecting main class for running
提问by Rogach
I have ~6 main classes in my application, by I usually use only one of them, so I wanted to run it automatically by sbt. sbt makes it possible to define two keys in build.sbt:
我的应用程序中有~6 个主要类,因为我通常只使用其中一个类,所以我想通过 sbt 自动运行它。sbt 可以在 build.sbt 中定义两个键:
// Run Key
val selectMainClass = TaskKey[Option[String]]("select-main-class", "Selects the main class to run.")
val mainClass = TaskKey[Option[String]]("main-class", "Defines the main class for packaging or running.")
so I defined them (sample project, two classes - Main1 & Main2 in the root of source dir):
所以我定义了它们(示例项目,两个类 - 源目录根目录中的 Main1 和 Main2):
mainClass := Some("Main1")
selectMainClass := Some("Main1")
And `show main-class' from sbt prompt also seems to work:
来自 sbt 提示的“show main-class”似乎也有效:
[info] Some(Main1)
But sbt's runtask still prompts me for main class.
但是sbt的run任务还是提示我主课。
Also, sbt-revolverfails to work with multiple classes with exception java.util.NoSuchElementException: None.get
此外,sbt-revolver无法与多个类一起使用,但有异常java.util.NoSuchElementException: None.get
Using sbt 0.11.2.
使用 sbt 0.11.2。
What am I doing wrong here?
我在这里做错了什么?
采纳答案by Heiko Seeberger
As you can see from using inspect, mainClassis scoped to various configurations and tasks:
从使用inspect可以看出,mainClass的范围是各种配置和任务:
> inspect compile:main-class(for run)
[info] Task: scala.Option[java.lang.String]
[info] Description:
[info] Defines the main class for packaging or running.
[info] Provided by:
[info] {file:/Users/heiko/tmp/}default-d7f1bf/compile:main-class(for run)
...
Therefore you have to use proper scoping:
因此,您必须使用适当的范围:
set mainClass in (Compile, run) := Some("Foo")
回答by radke
To prevent this:
为了防止这种情况:
sbt> ~run
Multiple main classes detected, select one to run:
[1] com.yourapp.MainClass1
[2] com.yourapp.MainClass2
[3] com.yourapp.MainClass3
do this:
做这个:
sbt> ~runMain com.yourapp.MainClass1
回答by Matt G
If you want to specify a main class for revolver specifically:
如果要专门为左轮手枪指定主类:
set mainClass in Revolver.reStart := Some("some.package.mainClass")
回答by Evhz
oneline console:
在线控制台:
sbt "run-main com.path.to.MainClass"

