Docs

Production Mode & Cloud Deployment to Heroku

Learn how to optimize your Hilla application for production and deploy it to Heroku

In this last chapter, you prepare the CRM application for production and deploy it to Heroku. The tutorial uses Heroku because it’s simple and comes with a free tier that doesn’t require a credit card when signing up. The free tier also includes PostgreSQL support.

Note
You can deploy Hilla applications on any cloud provider

You can deploy Hilla applications on any cloud provider or hosting service that supports Java. The project also contains a Dockerfile that you can use for container deployments. Learn more about deploying to different cloud providers.

This chapter covers:

  • Hilla production builds,

  • creating a Heroku account,

  • installing the Heroku CLI,

  • creating and deploying a Heroku application.

Preparing the Application for Production

It’s important to build a separate production-optimized version of the application before deploying it. In development mode, Hilla has a live-reload widget, debug logging, and uses a quick, but unoptimized, front-end build that includes source maps for easy debugging. Unoptimized front-end bundles can contain several megabytes of JavaScript.

The pom.xml build includes a production profile configuration that prepares an optimized build that’s ready for production.

Using a PostgreSQL Database in Production

During development, the application has used an in-memory H2 database. It’s convenient and works well for a single user. In production, you want to use something more robust and persistent. Heroku’s free tier supports PostgreSQL, so can configure your application to use that.

First, add the PostgreSQL dependency in the production profile of pom.xml:

<profile>
  <id>production</id>
  <!-- Omitted -->
  <dependencies>
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
    </dependency>
  </dependencies>
</profile>

Next, configure how JPA should handle schema generation. Add the following two properties to the end of application.properties.

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
Warning
Avoid Data Loss
This setup recreates the database on every deployment. If you are working with real data, you should use ddl-auto=none and instead use a database migration tool like Liquibase or Flyway so you can evolve the database schema without losing data.

Building a Production-Optimized JAR

Build the application with the production profile:

mvn clean package -Pproduction

This builds a production-optimized JAR file in the target folder.

Creating Heroku Account & Installing Heroku CLI

Complete the following steps to create a Heroku account and install the Heroku CLI.

  1. Go to https://signup.heroku.com/, create a new account, and verify your email.

  2. Go to https://devcenter.heroku.com/articles/heroku-cli and follow the instructions for installing the CLI on your operating system.

Deploying a Hilla Application to Heroku

Use the Heroku CLI to create and deploy your application.

  1. Log in:

    heroku login
  2. Configure the correct Java version:

    echo "java.runtime.version=11" > system.properties
  3. Install the Heroku Java plugin:

    heroku plugins:install java
  4. Create a new app. Replace APPNAME with a name of your choice. APPNAME is part of the URL, like https://APPNAME.herokuapp.com, so choose a name that’s unique and easy to remember.

    heroku create APPNAME
  5. Create a secret key for your application:

    heroku config:set APP_SECRET=$(openssl rand -base64 32)
  6. Enable the PostgreSQL plugin for the newly created app:

    heroku addons:create heroku-postgresql -a APPNAME
  7. Deploy the production-optimized JAR file you created in the previous section.

    heroku deploy:jar target/fusioncrmtutorial-1.0-SNAPSHOT.jar -a APPNAME
  8. Open the application in your browser.

    heroku open
  9. View the application logs and see if anything goes wrong.

    heroku logs --tail

Conclusion & Next Steps

Congratulations, you have now built a full-stack PWA and deployed it to Heroku.

Did you like the tutorial? Did you find anything that didn’t seem right? Reach out to me on Twitter @marcushellberg or Hilla’s Discord chat server.

Now that you have a running application, you can use it to experiment further or as a foundation for your next idea.

Happy hacking, and ping us @vaadin on Twitter to show off the cool stuff you have built.