Web Build Examples

This page contains examples of using Mill as a build tool for web-applications. It covers setting up a basic backend server with a variety of server frameworks

Jetty Hello World App

build.sc (download, browse)
import mill._, javalib._, publish._

object hello extends RootModule with JavaModule {
  def ivyDeps = Agg(
    ivy"org.eclipse.jetty:jetty-server:9.4.43.v20210629",
    ivy"javax.servlet:javax.servlet-api:4.0.1"
  )

  object test extends JavaModuleTests with TestModule.Junit4
}

This example demonstrates how to set up a simple Jetty webserver, able to handle a single HTTP request at / and reply with a single response.

> mill test
...HelloJettyTest.testHelloJetty finished...

> mill runBackground

> curl http://localhost:8085
...<h1>Hello, World!</h1>...

> mill clean runBackground

Spring Boot Hello World App

build.sc (download, browse)
import mill._, javalib._, publish._

object hello extends RootModule with JavaModule {
  def ivyDeps = Agg(
    ivy"org.springframework.boot:spring-boot-starter-web:2.5.6",
    ivy"org.springframework.boot:spring-boot-starter-actuator:2.5.6"
  )

  object test extends JavaModuleTests with TestModule.Junit5 {
    def ivyDeps = super.ivyDeps() ++ Agg(
      ivy"org.springframework.boot:spring-boot-starter-test:2.5.6"
    )
  }
}

This example demonstrates how to set up a simple Spring Boot webserver, able to handle a single HTTP request at / and reply with a single response.

> mill test
...com.example.HelloSpringBootTest#shouldReturnDefaultMessage() finished...

> mill runBackground

> curl http://localhost:8086
...<h1>Hello, World!</h1>...

> mill clean runBackground

Spring Boot TodoMvc App

build.sc (download, browse)
import mill._, javalib._, publish._

object hello extends RootModule with JavaModule {
  def ivyDeps = Agg(
    ivy"org.springframework.boot:spring-boot-starter-data-jpa:2.5.4",
    ivy"org.springframework.boot:spring-boot-starter-thymeleaf:2.5.4",
    ivy"org.springframework.boot:spring-boot-starter-validation:2.5.4",
    ivy"org.springframework.boot:spring-boot-starter-web:2.5.4",

    ivy"javax.xml.bind:jaxb-api:2.3.1",

    ivy"org.webjars:webjars-locator:0.41",
    ivy"org.webjars.npm:todomvc-common:1.0.5",
    ivy"org.webjars.npm:todomvc-app-css:2.4.1",

    ivy"com.h2database:h2:2.3.230"
  )

  object test extends JavaModuleTests with TestModule.Junit5 {
    def ivyDeps = super.ivyDeps() ++ Agg(
      ivy"org.springframework.boot:spring-boot-starter-test:2.5.6"
    )
  }
}

This is a larger example using Spring Boot, implementing the well known TodoMVC example app. Apart from running a webserver, this example also demonstrates:

  • Serving HTML templates using Thymeleaf

  • Serving static Javascript and CSS using Webjars

  • Querying a SQL database using JPA and H2

> mill test
...com.example.TodomvcApplicationTests#homePageLoads() finished...
...com.example.TodomvcApplicationTests#addNewTodoItem() finished...

> mill runBackground

> curl http://localhost:8087
...<h1>todos</h1>...

> mill clean runBackground