If you’re using the Scala Mill build tool and have a project that has multiple classes with main
methods, this tutorial shows how to choose a main method in the Mill REPL.
Starting Mill’s interactive mode (REPL)
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.
Selecting a main method
Inside the REPL you can choose a main
method to run 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 namedHello
- 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
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 you 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")()
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |
Step 6
Here’s a link to Step 6, Creating a Fat Jar with Mill (assembly). (For some reason the link isn’t showing up below, so I added it here manually.)