Deploying a Java Spring Boot Application to Heroku
A practical step-by-step guide to deploying a Spring Boot app to Heroku using the Heroku CLI and Git: from Maven configuration to cloud execution.
Deploying a Java application to the cloud can often feel complex, but Platform as a Service (PaaS) providers like Heroku make the deployment workflow straightforward and efficient.
In this guide, we will walk through the steps required to take a Spring Boot application (managed with Maven) and deploy it live to the cloud using the Heroku CLI and Git.
Step 1: Create a Heroku Account
The first step is setting up an account on the official Heroku platform. The dashboard will allow you to manage application dynos, databases, and environment variables.
Step 2: Install the Heroku CLI
To interact with cloud services directly from your local terminal, install the Heroku CLI.
Once installed, authenticate your terminal session:
heroku login
This command opens a browser window to complete the login process. You can verify that your SSH keys are correctly linked by running:
heroku keys
Step 3: Prepare the Spring Boot Application
If you don’t have a Spring Boot project ready, generate one using Spring Initializr:
- Choose a Maven Project with Java.
- Add the Spring Web dependency.
- Download and extract the project archetype onto your machine.
Ensure your pom.xml contains the Spring Boot Maven build plugin required to generate an executable .jar:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Step 4: Initialize the Git Repository
Heroku handles application deployments by integrating seamlessly with Git. Initialize a local Git repository within your project folder:
# Initialize repository and create initial commit
git init
git add .
git commit -m "Initial Spring Boot commit"
It is also recommended to host your source code on a remote provider like GitHub.
Step 5: Create the Heroku Application
From the root directory of your project, create a new Heroku cloud application:
heroku create
Heroku will provision a cloud instance with a randomly generated name and automatically attach a remote Git endpoint named heroku to your local repository.
To customize the app name, rename it at any time using:
heroku apps:rename my-custom-app-name
Step 6: Deploy the Application
To trigger the build build process and deploy your application, push your code to the Heroku remote branch:
git push heroku main
(Note: if your local primary branch is named master, use git push heroku master).
During execution, Heroku will automatically detect a Java/Maven project, resolve dependencies defined in pom.xml, compile the binary, and boot the application.
Step 7: Execution, Logging, and Monitoring
When the deployment finishes, launch the application in your default browser:
heroku open
Useful CLI Commands for Management:
- Stream live application logs:
heroku logs --tail - Restart dyno instances:
heroku restart
Whenever you update your code, commit your changes locally and run git push heroku main to deploy the latest version to the cloud.