Deploying a Vaadin Flow Application on Heroku
Caution
|
This tutorial is for Vaadin 14.
If this is your first time trying out Vaadin, you should read the same tutorial for the latest Vaadin version instead.
|
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
:
<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
Creating a Heroku Account and Installing the Heroku CLI
Complete the following steps to create a Heroku account and install the Heroku CLI.
-
Go to https://signup.heroku.com/, create a new account, and verify your email.
-
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.
-
Log in:
heroku login
-
Install the Heroku Java plugin:
heroku plugins:install java
-
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
-
Enable the PostgreSQL plugin for the newly created app:
heroku addons:create heroku-postgresql -a APPNAME
-
Deploy the production-optimized JAR file you created in the previous section.
heroku deploy:jar target/flowcrmtutorial-1.0-SNAPSHOT.jar -a APPNAME
-
Open the application in your browser.
heroku open
-
In the event that something goes wrong, view the application logs.
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/X @marcushellberg or visit the Vaadin Forum.
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/X to show off the cool stuff you’ve built!