如何在 SBT 0.13 项目中设置主类

问题描述:

你们能解释一下如何在 SBT 项目中设置主类吗?我正在尝试使用 0.13 版.

Could you guys please explain to me how to set main class in SBT project ? I'm trying to use version 0.13.

我的目录结构非常简单(与 SBT 的文档不同).在根文件夹中,我有 build.sbt 与以下内容

My directory structure is very simple (unlike SBT's documentation). In the root folder I have build.sbt with following content

name := "sbt_test"

version := "1.0"

scalaVersion := "2.10.1-local"

autoScalaLibrary := false

scalaHome := Some(file("/Program Files (x86)/scala/"))

mainClass := Some("Hi")

libraryDependencies ++= Seq(
    "org.scalatest" % "scalatest_2.10" % "2.0.M5b" % "test"
)

EclipseKeys.withSource := true

我有一个子文件夹 project 和单个文件 Hi.scala,其中包含以下代码

And I have subfolder project with single file Hi.scala which contains following code

object Hi {
  def main(args: Array[String]) = println("Hi!")
}

我可以通过调用 sbt compile 来编译它,但是 sbt run 返回

I'm able to compile it by calling sbt compile but sbt run returns

The system cannot find the file C:\work\externals\sbt\bin\sbtconfig.txt.
[info] Loading project definition from C:\work\test_projects\sbt_test\project
[info] Set current project to sbt_test (in build file:/C:/work/test_projects/sbt_test/)
java.lang.RuntimeException: No main class detected.
        at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed Apr 8, 2013 6:14:41 PM

您需要将您的应用程序的源代码放在 src/main/scala/ 中,project/ 用于构建定义代码.

You need to put your application's source in src/main/scala/, project/ is for build definition code.