Scala “Mill” build tool: How to specify the main method/class you want to run when you have multiple main methods

As another brief example of the Mill build tool, when you have multiple main methods in a Mill project you’ll need to specify which main method you want to run in your Mill build.sc configuration file. This is the build.sc syntax for doing that:

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 Mill build.sc file example

Here’s a complete Mill build.sc example for a small Scala project I’ve been working on that demonstrates this:

object RssAtomReader extends ScalaModule {

    def scalaVersion = "2.12.11"

    object test extends Tests {
        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")

}

The last line of that build.sc file shows how I define which Scala (or Java) class has the main method that I want to run.

This is just a short example, but for more information on Mill, see my Scala “Mill” build tool “Hello, world” example.