As another example of the Scala Mill build tool, when you have multiple main methods in a Scala/Mill project you’ll need to specify which main
method you want to run in your build.sc configuration file. If you don’t, you’ll see an error message like this:
1 targets failed HelloWorld.finalMainClass Multiple main classes found (hello.Hello,hello.Hello2) please explicitly specify which one to use by overriding mainClass
This is the build.sc syntax for overriding mainClass
:
def mainClass = Some("hello.Hello")
This says that you want to run the main
method that’s in the Hello
class that’s in the hello package.
A complete Scala Mill build.sc file example
Here’s a complete Mill build.sc example for a small Scala project I’ve been working on. The last line of this file demonstrates the use of mainClass
:
object RssAtomReader extends ScalaModule { def scalaVersion = "2.12.11" object test extends Tests { // dependencies for testing def ivyDeps = Agg( ivy"org.scalactic::scalactic:3.1.1", ivy"org.scalatest::scalatest:3.1.1" ) def testFrameworks = Seq("org.scalatest.tools.Framework") } // `::` for scala deps, `:` for java deps def ivyDeps = Agg( ivy"org.scala-lang.modules::scala-xml:1.0.6", ivy"com.typesafe:config:1.4.0", ivy"org.slf4j:slf4j-api:1.7.5", ivy"org.slf4j:slf4j-simple:1.7.5" ) // pass a -D command-line arg to my application def forkArgs = Seq("-Dconfig.file=RssAtomReader/src/main/resources/application.conf") // with mill you need to specify the main class when you have more than one def mainClass = Some("hello.Hello") }
In my project I have multiple main
methods, and the last line of this build.sc file shows how I define which Scala (or Java) class has the main
method that I want to run when I issue this command:
mill RssAtomReader
In a slightly related note, this is a JavaFX project, and in the forkArgs
line I show how to pass a JVM argument to my application when I run it.