Mill Build for Java Devs
Mill has its origin in the Scala world. However, it is well suited to build Java projects. Recently the official Mill documentation gained a growing Java section with many examples. So, I’m keeping this post short, as the official documentation has more information than I can cover.
You might be scared that Mill builds are written in Scala. Luckily, Mill is conservative with its Scala use to constructs most Java developers are already familiar with.
Build Questions answered by Mill builds
I’ve started this series with a list of questions you will encounter in non trivial builds. I claimed that Mill had great at answering most of these questions, but left the actual answers open. Well, after showing Mill for a bit, it is time to answer them.
Mill Basic Building Blocks
Lets explore the Mill building blocks. We start with a hello world:
import mill._
import scalalib._
def buildGreetings = T{ // T stands for Target ;)
val greeting = "Hello Mill!"
val outFile = T.dest / "greeting.txt" // Note, we use T.dest as our output directory
os.write(outFile, greeting)
println(s"File is in $outFile")
PathRef(outFile)
}
Then we build it:
$ ./mill buildGreetings
...
[1/1] buildGreetings
File is in /home/roman/dev/private-dev/mill-demo/out/buildGreetings.dest/greeting.txt
$ cat /home/roman/dev/private-dev/mill-demo/out/buildGreetings.dest/greeting.txt
Hello Mill!
That works. Note that we used T.dest
to give each target its own directory to write to disk
without trampling over other targets. The location in the 'out' directory is always the task names with a .dest
suffix.
Whirlwind Tour of Mill Build
Lets start a whirlwind tour. Last time we stopped at a basic Java app. Yes, Mill does of course support building Scala projects as well, but Mill works well for other builds as well.
class HelloWorld{
public static void main(String[] args){
System.out.println("I'm built with Mill")
}
}
Intro to Mill Build: Pleasant Complex Builds
I’ve experienced a few build tools over time: Apache Ant, Apache Maven, Gradle, SBT, MSBuild, make and probably some more I’ve forgotten about. Recently I’ve experimented with the Mill build tool and it is one of the best ones I’ve worked so far.
TLDR: In the Java eco system, I recommend to use Apache Maven when your app/library fits with its defaults. If Maven starts to be painful, consider Mill build and this blog series is for you ;).