Deploying a Vaadin Flow Application on Heroku

In this final chapter in the series, you learn how to deploy a Spring Boot application on Heroku.

This chapter covers:

  • Vaadin production builds.

  • Configuring PostgreSQL for production.

  • Creating a Heroku account.

  • Installing the Heroku CLI.

  • Creating and deploying a Heroku app.

Tip
Vaadin can be deployed on any cloud provider

You can also deploy your application onto other cloud platforms. Read the Cloud Deployment tutorials for more options.

Preparing the Application for Production

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

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

Using a PostgreSQL Database in Production

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

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

pom.xml

  1. <profile>
  2. <id>production</id>
  3. <!-- Omitted -->
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.postgresql</groupId>
  7. <artifactId>postgresql</artifactId>
  8. </dependency>
  9. </dependencies>
  10. </profile>

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

src/main/java/resources/application.properties

  1. spring.jpa.generate-ddl=true
  2. 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:

bash

  1. mvn clean package -Pproduction

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

Creating a Heroku Account and Installing the 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 Vaadin Application to Heroku

Use the Heroku CLI to create and deploy your application.

  1. Log in:

    terminal

    1. heroku login
  2. Install the Heroku Java plugin:

    terminal

    1. heroku plugins:install java
  3. 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.

    terminal

    1. heroku create APPNAME
  4. Enable the PostgreSQL plugin for the newly created app:

    terminal

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

    terminal

    1. heroku deploy:jar target/flowcrmtutorial-1.0-SNAPSHOT.jar -a APPNAME
  6. Open the application in your browser.

    terminal

    1. heroku open
  7. In the event that something goes wrong, view the application logs.

    terminal

    1. heroku logs --tail

Conclusion and 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 Vaadin’s Discord chat server.

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

Happy hacking, and ping us @vaadin on Twitter to show off the cool stuff you’ve built!

migration assistance

Download free e-book.
The complete guide is also available in an easy-to-follow PDF format.

https://pages.vaadin.com/en/build-a-modern-web-app-with-spring-boot-vaadin-pdf

Open in anew tab

Show code

render-banner.ts

  1. export class RenderBanner extends HTMLElement {
  2. connectedCallback() {
  3. this.renderBanner();
  4. }
  5. renderBanner() {
  6. let bannerWrapper = document.getElementById('tocBanner');
  7. if (bannerWrapper) {
  8. return;
  9. }
  10. let tocEl = document.getElementById('toc');
  11. // Add an empty ToC div in case page doesn't have one.
  12. if (!tocEl) {
  13. const pageTitle = document.querySelector(
  14. 'main > article > header[class^=PageHeader-module--pageHeader]'
  15. );
  16. tocEl = document.createElement('div');
  17. tocEl.classList.add('toc');
  18. pageTitle?.insertAdjacentElement('afterend', tocEl);
  19. }
  20. // Prepare banner container
  21. bannerWrapper = document.createElement('div');
  22. bannerWrapper.id = 'tocBanner';
  23. tocEl?.appendChild(bannerWrapper);
  24. // Banner elements
  25. const text = document.querySelector('.toc-banner-source-text')?.innerHTML;
  26. const link = document.querySelector('.toc-banner-source-link')?.textContent;
  27. const bannerHtml = `<div class='toc-banner'>
  28. <a href='${link}'>
  29. <div class="toc-banner--img"></div>
  30. <div class='toc-banner--content'>${text}</div>
  31. </a>
  32. </div>`;
  33. bannerWrapper.innerHTML = bannerHtml;
  34. // Add banner image
  35. const imgSource = document.querySelector('.toc-banner-source .image');
  36. const imgTarget = bannerWrapper.querySelector('.toc-banner--img');
  37. if (imgSource && imgTarget) {
  38. imgTarget.appendChild(imgSource);
  39. }
  40. }
  41. }