7

How to set mainClass in ScalaJS build.sbt?

Currently I set the main class in build.sbt like this (see last line):

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")

When I do sbt run, I get:

[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
>

It has no effect If I remove the mainClass setting from the build.sbt. Error message is same with the mainClass setting or without it.

On sbt prompt when I try runMain htmlExp.HtmlExpApp it works and runs corrent main class.

How can I set the mainClass in the build.sbt or is it so that ScalaJSPlugin does not support this setting?

user4955663
  • 1,039
  • 2
  • 13
  • 21
  • 1
    Hmmm... The `ScalaJSPlugin` definitely should support this setting. If you can point me to a repo I can have a look. Unfortunately, this looks like a bug. – gzm0 Jan 25 '16 at 04:10
  • Correction: Probably not a bug. See my answer. – gzm0 Jan 25 '16 at 04:13

1 Answers1

7

The mainClass needs to be set on a per-configuration basis:

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

If you just write mainClass := ???, it sets the mainClass for your whole project. However, mainClass detection is on a per-configuration basis. Therefore, the auto detection overrides you manual setting. The only way to properly override the auto detection is to set the mainClass for a specific configuration.

Note that this is nothing Scala.js specific at all. The same applies to normal JVM projects (for example, see this post).

Community
  • 1
  • 1
gzm0
  • 14,752
  • 1
  • 36
  • 64