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

查看:47
本文介绍了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天全站免登陆