Deploying Spring Boot apps to Heroku

Suben Kumer Saha
3 min readAug 1, 2021

--

Spring boot is by far the most popular java web framework. From Netflix, Intuit to thousand of small, medium and startup companies using spring boot for their new cool idea. Heroku, on the other hand, is one of the most popular PaaS(Platform as a Service) platform. It’s very popular among developer for their hobby project(due to it’s generous free hobby dyno) as well as serious project. In this tutorial we will discuss how to create and deploy a spring boot app in Heroku and some common pitfalls encountered along the way. Let’s begin!

Source: https://blog.heroku.com
  1. Generate a Spring Boot app: Go to https://start.spring.io/ and generate a new spring boot app.
spring boot initializr

2. Extract and open with your favorite IDE like IntelliJ IDEA.

3. Create a new app on Heroku.

Heroku: Create new app

4. Install Heroku CLI if you didn’t done already. If you need help checkout the following URL: https://devcenter.heroku.com/articles/heroku-cli

5. Login to Heroku using:

$ heroku login

6. Go to your project directory, initialize a git repository (for new project), add Heroku git repository as upstream in your git repository using the following commands:

$ cd my-project/
$ git init
$ heroku git:remote -a <app_name>

7. Now we need to make some changes to be able to deploy to Heroku smoothly. First change the port to ENV port as Heroku assign a dynamically generated port number for your app each time you deploy. For this, add the following code to your application.properties or application.yaml file with default port 8080 if no env port is set:

// in application.properties
server.port=${PORT:8080}
//or in application.yaml
server:
port: ${PORT:8080}

8. Create a file named system.properties at the root of your project directory with the following content:

java.runtime.version=11

By default, Heroku use JVM version 8, you can use whatever person is supported by Heroku as described at the following URL: https://devcenter.heroku.com/articles/java-support#supported-java-versions

9. Now add the following code snippet at the end of build.gradle file so that gradle don’t generate plain jar. Plain jar may cause class not found exception.

jar {
enabled = false
manifest {
attributes('Main-Class': 'com.example.demo.DemoApplication')
}
}

Also, set Main-Class attribute to your spring boot entry file it will solve Can’t execute jar- file: “no main manifest attribute” error.

For, kotlin DSL based settings.gradle.kts file add the following

tasks.getByName<Jar>("jar") {
enabled = false
manifest {
attributes["Main-Class"] = "com.example.demo.DemoApplicationKt"
}
}

10. (Optional) add a `Procfile’ at the project root directory with the following command.

web: java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

change the file path according to your project name and settings.

11. Now, add the files to git and commit, and PUSH!!

$ git add .
$ git commit -am "deploying to heroku"
$ git push heroku master

12. Check the logs in case of any error:

heroku logs --tail

If everything goes well, you will see the following screen at your application URL (as there is nothing in base route):

spring demo landing page

The complete repository will be available at https://github.com/subenksaha/spring-heroku

Happy Coding!!!

--

--