sbt:选择主类进行运行 [英] sbt: selecting main class for running

查看:13
本文介绍了sbt:选择主类进行运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有大约 6 个主要类,我通常只使用其中一个,所以我想通过 sbt 自动运行它.sbt 可以在 build.sbt 中定义两个键:

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:

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

所以我定义了它们(示例项目,两个类 - Main1 和 Main2 在源目录的根目录中):

so I defined them (sample project, two classes - Main1 & Main2 in the root of source dir):

mainClass := Some("Main1")

selectMainClass := Some("Main1") 

而且 sbt 提示符中的 `show main-class' 似乎也可以工作:

And `show main-class' from sbt prompt also seems to work:

[info] Some(Main1)

但是 sbt 的 run 任务仍然提示我进入主类.

But sbt's run task still prompts me for main class.

此外,sbt-revolver 无法处理多个类,但出现异常 java.util.NoSuchElementException: None.get

Also, sbt-revolver fails to work with multiple classes with exception java.util.NoSuchElementException: None.get

使用 sbt 0.11.2.

Using sbt 0.11.2.

我在这里做错了什么?

推荐答案

从使用 inspect 可以看出,mainClass 的范围是各种配置和任务:

As you can see from using inspect, mainClass is scoped to various configurations and tasks:

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

这篇关于sbt:选择主类进行运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆