如何在ScalaJS build.sbt中设置mainClass?

时间:2016-01-23 15:10:08

标签: scala sbt main scala.js

如何在ScalaJS build.sbt中设置mainClass?

目前我在build.sbt中设置了这样的主类(参见最后一行):

enablePlugins(ScalaJSPlugin)

name := "ScalaJS-Exp"

scalaVersion := "2.11.7"

libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.8.1" 
libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.8.0" 

jsDependencies += RuntimeDOM

skip in packageJSDependencies := false

//scalaJSStage in Global := FastOptStage

// uTest settings
libraryDependencies ++= Seq(
    "com.lihaoyi" %%% "utest" % "0.3.1" % "test",
    "com.lihaoyi" %%% "scalatags" % "0.5.4",
    // Javascript libs
    "org.webjars" % "jquery" % "1.10.2",
    "org.webjars" % "jquery-ui" % "1.11.4"
)

jsDependencies ++= Seq(
    "org.webjars" % "jquery" % "1.10.2" / "jquery.js",
    "org.webjars" % "jquery-ui" % "1.11.4" / "jquery-ui.js" dependsOn "jquery.js"
)

testFrameworks += new TestFramework("utest.runner.Framework")

persistLauncher in Compile := true
persistLauncher in Test := false

mainClass := Some("htmlExp.HtmlExpApp")

当我跑步时,我得到:

[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list
[trace] Stack trace suppressed: run last compile:packageScalaJSLauncher for the full output.
[error] (compile:packageScalaJSLauncher) Cannot write launcher file, since there is no or multiple mainClasses
[error] Total time: 1 s, completed Jan 23, 2016 4:36:02 PM
> show discoveredMainClasses
[info] List(htmlExp.HtmlExpApp, tutorial.webapp.MyApp)
[success] Total time: 0 s, completed Jan 23, 2016 4:36:13 PM
>

它没有效果如果我从build.sbt中删除mainClass设置。错误消息与mainClass设置相同或没有。

在sbt提示符下,当我尝试运行htmlExp.HtmlExpApp时,它可以运行并运行相应的主类。

如何在build.sbt中设置mainClass,还是让ScalaJSPlugin不支持此设置?

1 个答案:

答案 0 :(得分:7)

mainClass需要按配置设置:

mainClass in Compile := Some("htmlExp.HtmlExpApp")

如果你只是写mainClass := ???,它会为整个项目设置mainClass。但是,mainClass检测基于每个配置。因此,自动检测会覆盖您手动设置。正确覆盖自动检测的唯一方法是为特定配置设置mainClass

请注意,这根本不是Scala.js所特有的。这同样适用于普通的JVM项目(例如,请参阅this post)。