Deploying Java Applications to Heroku from Eclipse or IntelliJ IDEA
Last updated April 10, 2020
Table of Contents
Many Java developers find that deploying from an IDE is a more natural fit than deploying from the command-line. In this article, you’ll learn how to deploy to Heroku from two popular Java-based IDEs: Eclipse and IntelliJ IDEA. In both cases, you’ll use the Heroku Maven Plugin to do the heavy lifting.
Setting up the Heroku Maven Plugin
If you have an existing project using the Heroku Maven Plugin, then you can use it for the steps in this article. If you do not, then start by cloning the sample project:
$ git clone https://github.com/kissaten/maven-plugin-war-example
Then create a new Heroku application by running the following command:
$ heroku create
Creating gentle-shore-6874... done, stack is heroku-18
https://gentle-shore-6874.herokuapp.com/ | git@heroku.com:gentle-shore-6874.git
Next, you’ll need to have one one of the IDEs installed. You can download and install Eclipse as describe on the Eclipse download site. For IntelliJ, you can download and install either the free Community edition or begin a free-trial of the Ultimate Edition. Once your IDE is installed, you can use it to open your project.
To open your project in Eclipse, select File > Import...
from the Eclipse menu. Then choose Maven
and below that, Existing Maven Projects
. Click Next
, and enter the location of the Git project you cloned for “Root directory”. Then click Finish
. After a few seconds, your project will appear in the “Package Explorer”
To open your project in IntelliJ IDEA, choose File > Open
and then select the directory location of the Git project you cloned.
Deploying from Eclipse
You can watch a short video demonstration of Eclipse deployment.
In the “Package Explorer” pane (usually on the left side of the window), expand the project so you can see its files. Then right-click on the pom.xml
and select Run As > Maven build...
.
In the dialog that appears, enter heroku:deploy-war
in the “Goals” field, as shown here.
If you were deploying a standalone Java application, you could use the heroku:deploy
target to deploy without a WAR file. For more information on deploying standalone apps, see the Heroku Maven Plugin documentation.
On some platforms, including Windows, you will also need to set the HEROKU_API_KEY
environment variable for the task. Click the Environment
tab, and select New...
to create a new variable. Enter “HEROKU_API_KEY” for the name and your API key for the value. You can get your API key by running heroku auth:token
at the command-line.
Now click Run
, and the deployment will start. In the “Console” pane you will see something like this:
Open your application by running $ heroku open -a <app-name>
from the command-line, or by browsing to https://<app-name>.herokuapp.com
(but replace <app-name
with the name of your app).
Generating launch configurations
You can optionally run a Maven command (either from the terminal or from Eclipse) to generate launch configurations for common goals:
$ mvn heroku:eclipse-launch-config
The will create a number of *.launch
files in the root of your project. You can click these files in Eclipse to run them.
Deploying from IntelliJ IDEA
Before opening IntelliJ, ensure that your M2_HOME
environment variable is set to the location of your Maven installation. If you would prefer not to set this variable, then you may need to configure the path to your Maven installation under Preferences
-> Maven
within IntelliJ.
To deploy, select Run
-> Edit Configurations
from the IntelliJ IDEA menu. Then select the +
button to create a new Maven configuration.
Then name the configuration “Heroku Deploy”, and enter heroku:deploy-war
in the “Command Line” field as shown here.
If you were deploying a standalone Java application, you could use the heroku:deploy
target to deploy without a WAR file. For more information on deploying standalone apps, see the Heroku Maven Plugin documentation.
Click OK
. Now a “Heroku Deploy” task will be shown in the Run Configurations toolbar. Press the green arrow button next to it.
In the “Console” pane you will see something like this:
Open your application by running $ heroku open -a <app-name>
from the command-line, or by browsing to https://<app-name>.herokuapp.com
(replace <app-name
with the name of your app).
Troubleshooting
If you receive the error Failed to deploy application: Forbidden
, such as:
[ERROR] Failed to execute goal com.heroku.sdk:heroku-maven-plugin:0.4.1:deploy-war (default-cli) on project helloworld: Failed to deploy application: Forbidden -> [Help 1]
This indicates that the Maven plugin cannot find your app. Check that you have a “heroku” remote in your Git repository by running this command:
$ git remote
heroku
origin
Then try adding your application name to the pom.xml
in the plugin configuration like this:
<configuration>
<appName>gentle-shore-6874</appName>
</configuration>
Your app name will be something other than “gentle-shore-6874”.
If you receive the error Failed to deploy application: Unauthorized
, such as:
[ERROR] Failed to execute goal com.heroku.sdk:heroku-maven-plugin:0.4.1:deploy-war (default-cli) on project helloworld: Failed to deploy application: Unauthorized -> [Help 1]
This means that you are not authorized to deploy to the configured application. Check that you are logged into the toolbelt by running this command:
$ heroku auth:whoami
ide-test@heroku.com
Then check that the value in your HEROKU_API_KEY
is correct.
For more information on using Maven with Eclipse, see the Eclipse documentation. For more information on using Maven with IntelliJ IDEA, see the IntelliJ documentation.
For more information on the Heroku Maven plugin, see the DevCenter article Deploying Java Applications with the Heroku Maven Plugin.