繁体   English   中英

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

[英]how to set main class in SBT 0.13 project

你们能解释一下如何在 SBT 项目中设置主要的 class 吗? 我正在尝试使用 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

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

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

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

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

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

这是指定主类的方法

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

对于 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 时的挫败感.....

如果您的项目中有多个主要方法,您可以将以下行添加到您的 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"

我有同样的问题:是模式遵循http://www.scala-sbt.org/0.13/docs/Hello.html的教程,在我看来,作为构建工具sbt的交互和错误消息可以是对新人相当误导。

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

有4个选项

  1. 你有 1 个主要课程

    • sbt run和 sbt 会为你找到 main
  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("abDummyMain1")
  2. 您可以使用主类作为参数运行

    sbt runMain abDummyMain1

如果 Main 类在不同的项目中,那么通过在build.sbt设置以下命令将起作用:

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

我遇到过同样的问题。 build.sbt添加PlayMinimalJava插件后解决了它。

不确定它是如何修复的,如果有人能强调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)

如果有人像我一样犯了愚蠢的错误。

检查您的项目是否是 sbt-multi 项目。 如果确保提供sbt moduleName/run而不是sbt run

当我在多个项目和单个项目之间切换时,我总是犯这个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM