Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the mainClass setting work in sbt?

Tags:

scala

sbt

I can't seem to find any details of how the mainClass option works in the build configuration of sbt. You specify the name of a class to use when you invoke the run action, but what does it actually do with it? Does it call a method on the class?

like image 905
ryeguy Avatar asked Mar 05 '11 23:03

ryeguy


Video Answer


1 Answers

http://code.google.com/p/simple-build-tool/wiki/BuildConfiguration#Run_Options

Method mainClass is of type Option[String] and specifies an optional main class to run when the run task is invoked. The default implementation specifies no main class (None). When mainClass is not specified, the run task will determine which class to run automatically. If exactly one main class is detected, it is run. If multiple main classes are detected, the user is prompted for which one to run.

The class name is expected to refer to an object of the same name that has a def main(args:Array[String]): Unit method. That method is run.

So if you create

package foo
object Foo { def main(args:Array[String]) { println("foo") } }

You can then use override def mainClass = Some("foo.Foo") so that the run target would run foo.Foo.

like image 156
huynhjl Avatar answered Oct 02 '22 14:10

huynhjl