Deploying Hilla apps as JAR file

René Wilby | Jun 10, 2024 min read

Build Hilla app for deployment

A Hilla app that was created using the Hilla CLI, can be build as a JAR file for deployment using Maven (or Gradle):

./mvnw clean package -Pproduction

The created JAR file contains all required classes, dependencies and the frontend bundle. As a Hilla app is a classic Spring Boot project, the JAR file created also contains an integrated server that serves the Hilla app as soon as the JAR file is executed.

Alternatively, a Hilla app can also be built as a WAR file and deployed via a servlet container.

Execute Hilla app as JAR file

The built Hilla app my-app can be executed in a compatible Java runtime environment as follows:

java -jar target/my-app.jar

Spring Boot specific configurations can be set or overridden as environment variables if required. For example, it is possible to adjust the log level at the start of the application as follows:

java -jar -Dlogging.level.com.vaadin=DEBUG target/my-app.jar

Create Docker image for Hilla app

Applications are often deployed based on containers. It is therefore advisable to create a Dockerfile for the Hilla app. The Dockerfile should be located in the base directory of the Hilla app and have the following content:

FROM eclipse-temurin:21-jre

COPY target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/app.jar"]

The Dockerfile contains the required Java runtime environment, the JAR file that is copied into the Docker image, and the configuration for the port via which the application can receive requests. The ENTRYPOINT points to the JAR file in the Docker image that is executed when the container is started.

Create Docker image locally

The Docker image can be created locally with Docker or Podman:

docker|podman build --tag my-app .

The command is executed in the base directory of the Hilla app. The . points to the same directory and the Dockerfile in it.

The Docker image is then available and usable locally.

Run Docker container locally

A Docker container with the Hilla app can be started based on the Docker image created:

docker|podman run -it --rm --publish 8080:8080 my-app

Deploy Hilla app to fly.io

With the help of the Dockerfile created, a Hilla app can be deployed very easily via a service such as fly.io. The registration at fly.io is free, but requires credit card details. Fly.io offers a free hobby plan, which allows you to deploy applications with limited resources free of charge. The app can then be accessed via https://<unique-app-identifier>.fly.dev. The necessary configuration of IP addresses, DNS, SSL certificate etc. is handled by fly.io.

The path to deployment is described in detail in https://fly.io/docs/hands-on/.

First, the flyctl is installed. Under macOS, this can be done with brew, for example:

brew install flyctl

Afterwards, a new account is created with fly.io or an existing account is linked:

fly auth signup

A deployment configuration for fly.io is created before the first deployment. This is done using the command:

fly launch --dockerfile Dockerfile

The suggested settings can be adopted. A .dockerignore should not be created initially, as this would prevent the JAR file from being copied for the time being.

flyctl stores the deployment configuration in the file fly.toml. This file can be edited if required.

app = '<unique-app-identifier>'
primary_region = 'ams'

[build]
  dockerfile = 'Dockerfile'

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[[vm]]
  size = 'shared-cpu-1x'
  memory = '512mb'

The actual deployment of the Hilla app is then carried out using:

fly deploy

The Dockerfile and the JAR file are uploaded to a build server of fly.io. The Docker image is created at fly.io. Fly.io stores the created Docker image in its own registry and then starts the Hilla app as a Docker container. After a successful start, the app can be called up in the browser via https://<unique-app-identifier>.fly.dev.

Update Hilla app

If changes are made to the code of the Hilla app, it must be rebuilt and then deployed again:

./mvnw clean package -Pproduction
fly deploy

Summary

A Hilla app can be built very easily for deployment as a JAR file. With the help of a suitable Dockerfile, the deployment via a service such as fly.io is not a problem. The official Hilla documentation offers further instructions for deploying Hilla apps, e.g. to AWS, GCP, Azure or Heroku.