Mill build tool: How to select a main method/class in the Mill REPL (interactive mode)

If you’re using the Mill build tool and have a project that has multiple classes with main methods, you can choose a main method to run when you’re using Mill’s interactive mode (REPL) with this syntax:

@ RssAtomReader.runMain("hello.Hello")()

This example assumes:

  • My project subdirectory is named RssAtomReader
  • I have multiple main methods in my project
  • The main method I want to run is in a class named Hello
  • The Hello class is in a package named hello

In this case I’m using a main method in a Scala class, but it could also be a main method in a Java class.

Mill’s interactive mode (REPL)

You can see this in the Mill REPL by typing the name of your project and then hitting the [Enter] key. For example, my project is named RssAtomReader, so I type that in the Mill REPL and then hit [Enter], and see this:

@ RssAtomReader

res0: RssAtomReader = ammonite.predef.build#RssAtomReader:4
Children:
    .test
Commands:
    .ideaJavaModuleFacets(ideaConfigVersion: Int)()
    .ideaConfigFiles(ideaConfigVersion: Int)()
    .ivyDepsTree(inverse: Boolean, withCompile: Boolean, withRuntime: Boolean)()
    .runLocal(args: String*)()
    .run(args: String*)()
    .runBackground(args: String*)()
    .runMainBackground(mainClass: String, args: String*)()
    .runMainLocal(mainClass: String, args: String*)()
    .runMain(mainClass: String, args: String*)()
    .console()()
    .repl(replOptions: String*)()
Targets:
    .allSourceFiles()

The Mill output shows this as part of its output:

.runMain(mainClass: String, args: String*)()

That output is what tells me that I can choose which main class to run:

@ RssAtomReader.runMain("hello.Hello")()

If I instead wanted to run the main method in a class named Foo in a package named foo, I’d use this syntax:

@ RssAtomReader.runMain("foo.Foo")()

Passing command-line arguments to your main class

I haven’t tested this next part yet, but if I want to pass command-line arguments to that class, it looks like this is the correct syntax for that:

@ RssAtomReader.runMain("foo.Foo", "bar", "baz")()

Mill interactive mode

I skipped over this in the beginning of this tutorial, but you start Mill’s interactive mode from your operating system command-line with this mill command in the root directory of your project:

$ mill -i

That command starts the Mill REPL, where you can work with Mill interactively.

... this post is sponsored by my books ...
 

Summary

As a quick summary, if you’re using the Mill build tool and have a project that has multiple classes with main methods, and you’re using Mill’s interactive mode (REPL), this little tutorial demonstrates how you can choose which main method to run, and also shows how to pass command-line arguments to that main method.