How to deploy to Heroku

João Duarte Pinto
4 min readMay 28, 2021

Deploying a Spring Boot Application WAR file to Heroku with Postgres database

What is Heroku?

Heroku is a cloud platform that enables companies to spend less time worried about infrastructure and deploying apps that immediately start producing value.

How Heroku works?

Heroku allows you to deploy applications in several languages.

Whatever the type of application, it will not be necessary to change the code to be able to be run by Heroku.

It is only necessary to inform which part of the application is executable (defined in the Procfile file at the root of the project).

Heroku allows you to deploy via various platforms or tools, such as Git, Github, Hashicorp Terraform, War deployment or Docker-based deployments.

In general, Heroku is an easy-to-use tool and there is a lot of documentation to help with its application.

Deploying a WAR file to heroku, with Postgres database

First of all, we must create an account on Heroku, through this link:

heroku.com

As my operating system is Mac OS, for the installation of Heroku, the following command was used:

$ brew tap heroku/brew && brew install heroku

The rest of the alternatives can be found here.

Create an App at Heroku.com

The process of creating an application is simple, just choose a name!

Prepare project to deploy

Create database schema

We will need to generate the database schema so that we can enter it directly into the database, so we add the following

content to the application.properties file and run the application. It will generate a file called create.sql that will

contain the commands for the database setup.

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadataspring.jpa.properties.javax.persistence.schema-generation.scripts.action=createspring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sqlspring.jpa.properties.hibernate.hbm2ddl.delimiter=;

Postgres connection and setup

Now, on heroku.com within the application we are going to add the Heroku Postgres add-on. The option is found on the Resources page.

Now we go to Settings, and we show the Config Var, the link of the database will be shown.

Let’s copy the link and decompose it, so that we can access the database through pgAdmin 4 (instructions for its installation can be found here).

In pgAdmin 4 we must create a new server and fill in according to the following images (the names are based on the decomposed link).

Then, we open the query tool and run the commands created and contained in the create.sql file.

Schema example

Changes to application.properties

In the application.properties file, we should comment out everything that is not necessary and define the url of the data source with the environment variable found in the Heroku container.

This will make use of the environment variable that contains the database url.

spring.datasource.url=${DATABASE_URL}

Changes to build.gradle

In the dependencies of build.gradle you have to add the dependencies of postgres and spring jbdc, and comment on the dependency for H2:

dependencies {implementation ‘org.springframework.boot:spring-boot-starter-data-jpa’implementation ‘org.springframework.boot:spring-boot-starter-data-rest’implementation ‘org.springframework.boot:spring-boot-starter-thymeleaf’//runtimeOnly ‘com.h2database:h2’testImplementation ‘org.springframework.boot:spring-boot-starter-test’providedRuntime ‘org.springframework.boot:spring-boot-starter-tomcat’// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbcimplementation group: ‘org.springframework.boot’, name: ‘spring-boot-starter-jdbc’, version: ‘2.4.5’// https://mvnrepository.com/artifact/org.postgresql/postgresqlimplementation group: ‘org.postgresql’, name: ‘postgresql’, version: ‘42.2.18’}

Deploy

So let’s take care of the build and deploy!

In the terminal, at the base of the project we will log in to Heroku:

$ heroku login

Next, let’s build the project:

$ ./gradlew build

To deploy the war file created in the build we have to install the Heroku Java CLI plugin:

$ heroku plugins:install java

Now we just need to deploy!

$ heroku war:deploy <path_to_war_file> — app <app_name>

If the Gods had us, the app will go online and make use of the external database.

--

--