Deploy Heroku app using Gradle

  • Tutorial
In continuation of the previous article about the deployment of Ratpack applications on Heroku, today I will talk about using the Gradle plugin. It was a difficult experience deploying a simple Ratpack app on Heroku PaaS. We rummaged through the buildpack's intricacies for deployment on Heroku. The good news is that this is not necessary.
In this article, I will show an easier way to deploy. This method does not need to explicitly use the Heroku Toolbelt and custom buildpack's . You also no longer need to have Ruby, RVM, or many other Ruby-related technologies that Java / Groovy developers do not need, they do not even need to know about this.

All this thanks to the excellent Java API that Heroku has made available for us to use. This is a simple Java library that allows us to interact directly with Heroku. Added the ability to work with Git repositories through the JGit library provided by the Eclipse Foundation, and we have everything we need to deploy our application from the directory with our project. The advantage is that it is all wrapped in a Gradle plugin that manages everything on your behalf.
In fact, all we need is an installed JDK, with environment variables in PATH, an installed Gradle and a good text editor. Well, let's get started:

Configuration

First of all, we need to configure our build.gradle project file as follows:

buildscript {
    repositories {
        ...
        mavenCentral()
        maven { url 'http://dl.bintray.com/vermeulen-mp/gradle-plugins' }
    }
    dependencies {
        classpath "org.gradle.api.plugins:gradle-heroku:0.9.6"
    }
}
apply plugin: 'heroku'
...
heroku {
    //get this from heroku
    apiKey = 'my-api-key'
    //set this on first run if you don't want a generated name
    //appName = 'some-unique-app-name'
    //set this if you are not happy with the default gradlew buildpack
    //buildpack = 'http://somebuildpack
}


This is the setup of our build to use the heroku plugin. Pay attention to the heroku code block, the plugin configuration is described there. The only required field here is apiKey. The appName field is not required for the first launch, but must be specified for subsequent launches. If this field is not specified, then Heroku will generate the application name on your behalf and will link your local git repository to the application instance on Heroku. If you do not like the generated names, you can name your application explicitly the first time you run it.
It is also worth noting that apiKey can be extracted from the properties file to solve security problems.
Next we need to add a Procfileto the root folder of our project. This is a small file responsible for the initial loading of the application when it is deployed. The essential feature here is that the commands start with of web: . This will depend on which application you are trying to deploy.

Fat Jar apps (Spring Boot and DropWizard)

Create a Procfile in the root folder of our project, add the following contents:
---
default_process_types:
  web: java -jar -Dport=$PORT build/libs/my-springboot-fat.jar


Running this command for a Spring Boot or Dropwizard application using fat.jar is sufficient to start the application.

Ratpack

Deploying Ratpack is almost as simple as that, but it requires a bit more tweaking. First, you need root access to the project root directory, which Gradle uses to name in the startup script. This can be done by adding the settings.gradle file to the root folder of your project.

The contents of the settings.gradle file :
rootProject.name = 'ratpack'


Next we add our Procfile :
---
default_process_types:
  web: export RATPACK_OPTS="-Dratpack.port=$PORT" && build/install/ratpack/bin/ratpack build/install/ratpack/ratpack.groovy


Buildpacks

If you did not specify buildpack in the heroku code block in your build.gradle file , then the plugin will select default buildpack . This buildpack will be used by the gradle wrapper in your project to prepare your application for deployment. It does the bare minimum, just executes the gradlew command in the project directory. It also requires that defaultTasks be installed within your build file.

Default tasks

In order for default buildpack to know which tasks to run, we must specify the list of tasks in the build.gradle file . For example, to prepare the Ratpack application for deployment, you must add the following tasks:
defaultTasks "clean", "build", "installApp"


Accept the JDK!

The default buildpack allows us to even choose which JDK to use in runtime. Just put the system.properties file in the project’s base folder with the following contents:
java.runtime.version=1.7


If the system.properties file is not present in your project, then buildpack assumes JDK 1.8 on your behalf.

Get started!

So far, everything was for configuration, now we will launch our application. Open the terminal and enter the following commands:
$ gradle tasks
...
herokuAppCreate - Creates a new application on Heroku.
herokuAppDeploy - Deploy the application to Heroku.
herokuAppDestroy - Destroy the application on Heroku.
herokuAppInfo - Displays comprehensive information about the named application.
herokuAppList - Lists all Apps available for the current user on Heroku.
herokuBuildpack - Downloads and explodes the specified buildpack to this project.
...


Now we know that we can begin to deploy. Let's start by adding wrapper to our git:
$ ./gradlew wrapper
$ git add gradlew gradle
$ git commit -m 'Add wrapper.'


Next, create an application on Heroku and deploy it:
$ ./gradlew herokuCreateApp
$ ./gradlew herokuDeployApp


If all is well, then you should now have a deployed application on Heroku. You can find the source
code for the gradle plugin and default builpack on GitHub.
That's all!
We saw how easy it is to get the application deployed on Heroku using the Gradle project. All we need is apiKey and some very simple settings to get our application singing sweetly in the clouds. Deploying an application on Heroku has never been easier! The original article is here . Thanks for attention.

Also popular now: