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

时间:2013-04-09 01:16:15

标签: scala sbt

请问您能否向我解释如何在SBT项目中设置主要课程?我正在尝试使用版本0.13。

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

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,其中包含以下代码

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

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

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

9 个答案:

答案 0 :(得分:45)

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

答案 1 :(得分:23)

尝试使用对象并从App扩展它而不是使用类

object Main extends App {
  println("Hello from main scala object")
}

因为你需要运行main方法既不是主类

答案 2 :(得分:16)

以下是指定主类

的方法

mainClass in (Compile,run) := Some("my.fully.qualified.MainClassName")

答案 3 :(得分:6)

对于SBT(0.13)中的自定义模块,只需在SBT控制台上输入:

 project moduleX
 [info] Set current project to moduleX (in build file:/path/to/Projects/)
 >   run
 [info] Running main 

将范围切换到moduleX,如在Built.scala中定义。将自动检测该范围​​内的所有主要类。否则你会得到同样的错误,没有检测到主类。 看在上帝的意义上,SBT没有告诉你没有设置默认范围。它与默认源文件夹和非默认源文件夹无关,但只有SBT没有告诉任何它不知道默认使用哪个模块。

对类型安全的大提示:请添加默认输出,如:

[info] Project module is not set. Please use ''project moduleX''  set scope 
or set in Built file (LinkToDocu)  
在SBT结束时开始降低在多模块项目上使用SBT时的挫折感......

答案 4 :(得分:3)

如果项目中有多个主要方法,可以将以下行添加到build.sbt文件中:

val projectMainClass = "com.saeed.ApplicationMain"

mainClass in (Compile, run) := Some(projectMainClass)

如果要在将应用程序打包为JAR文件时指定将添加到清单中的类,请将此行添加到build.sbt文件中:

mainClass in (Compile, packageBin) := Some(projectMainClass)

您还可以在sbt和activator中使用run-main命令指定主类来运行:

sbt "run-main com.saeed.ApplicationMain"

activator "run-main com.saeed.ApplicationMain"

答案 5 :(得分:1)

我遇到了同样的问题:模式遵循http://www.scala-sbt.org/0.13/docs/Hello.html的教程,在我看来,作为构建工具sbt的交互和错误消息可能会误导新手。< / p>

事实证明,以后几个小时的头部刮伤,我每次都错过了示例中的关键cd hello行。 : - (

答案 6 :(得分:0)

有4个选项

  1. 您有1个主班

    • sbt run和sbt将为您找到主要对象
  2. 您有2个或更多主要班级

    • sbt run和sbt会建议选择要运行的那个。

Multiple main classes detected, select one to run:
[1] a.b.DummyMain1
[2] a.b.DummyMain2
Enter number:

  1. 您要手动设置主类。

    mainClass in run := Some("a.b.DummyMain1")
    
  2. 您可以将主类作为参数运行

    sbt runMain a.b.DummyMain1
    

答案 7 :(得分:0)

如果Main类位于其他项目中,则可以通过在build.sbt中设置以下命令来工作:

addCommandAlias("run", "; project_folder/run")

答案 8 :(得分:0)

我有同样的问题。在PlayMinimalJava中添加build.sbt插件后解决了该问题。

不确定它是如何修复的,如果有人可以强调PlayMinimalJava是如何解决的,那将是很好的。

enablePlugins(PlayMinimalJava)

我的build.sbt看起来像这样

organization := "org"
version := "1.0-SNAPSHOT"
scalaVersion := "2.13.1"
libraryDependencies += guice
enablePlugins(PlayMinimalJava)

日志

C:\Users\KulwantSingh\repository\pdfdemo>sbt run
Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading settings for project pdfdemo-build from plugins.sbt ...
[info] Loading project definition from C:\Users\KulwantSingh\repository\pdfdemo\project
[info] Loading settings for project pdfdemo from build.sbt ...
[info] Set current project to pdfdemo (in build file:/C:/Users/KulwantSingh/repository/pdfdemo/)
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Enter to stop and go back to the console...)

[info] Compiling 6 Scala sources and 2 Java sources to C:\Users\KulwantSingh\repository\pdfdemo\target\scala-2.13\classes ...
[info] p.a.h.EnabledFilters - Enabled Filters (see <https://www.playframework.com/documentation/latest/Filters>):

    play.filters.csrf.CSRFFilter
    play.filters.headers.SecurityHeadersFilter
    play.filters.hosts.AllowedHostsFilter

[info] play.api.Play - Application started (Dev) (no global state)
相关问题