📜 ⬆️ ⬇️

Deploy the application to Heroku using Gradle

In continuation of the previous article about the deployment of Ratpack applications on Heroku, today I will tell you about using the Gradle plugin. It was a difficult experience to deploy a simple Ratpack application on Heroku PaaS. We rummaged through the buildpack's intricacies for deployment on Heroku. The good news is that this is not necessarily bigger.
In this article, I will show an easier way to deploy. This method does not need to explicitly use the Heroku Toolbelt and custom buildpacks . Also, you no longer need to have Ruby, RVM, or many other Ruby-related technologies that are not needed by Java / Groovy developers, they don’t even need to know about this.

All this thanks to the excellent Java API that Heroku has made available for use by us. 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 all this is wrapped in a Gradle plugin that manages everything on your behalf.
In fact, all we need is the installed JDK, with environment variables in PATH, the installed Gradle, and a good text editor. Well, let's get started:

Configuration

First of all, we need to configure our project build.gradle 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 setting up our build to use the heroku plugin. Pay attention to the block of code heroku, there is described the configuration of the plugin. The only required field here is apiKey. The appName field is optional for the first run, but must be specified for subsequent launches. If this field is not specified, Heroku will generate the application name on your behalf and will associate your local git repository with the application instance on Heroku. If you don’t like the generated names, you can call your application explicitly when you first start it.
It is also worth noting that apiKey can be extracted from the properties file to resolve security issues.
Next, we need to add the Procfile to the root folder of our project. This is a small file responsible for booting the application when it is deployed. The essential feature here is that commands begin with 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 content:
 --- 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 the Ratpack application is almost as simple, but it requires a bit more tweaking. First, you need root rights to the root directory of the project, which Gradle uses for naming in the startup script. This can be done by adding the settings.gradle file to the root folder of your project.

Contents of 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 the buildpack in the heroku code block in your build.gradle file, the plugin will select the default buildpack . This buildpack will be used by the gradle wrapper in your project to prepare your application for deployment. He does the bare minimum, just runs the gradlew command in the project directory. It also requires that defaultTasks be installed within your build file.

Default Tasks

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


Take JDK!

Default buildpack allows us to even choose which JDK to use at runtime. Simply place the system.properties file in the base project folder with the following content:
 java.runtime.version=1.7 


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

Proceed to launch!

Until now, 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 proceed with the deployment. Let's start by adding a 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, now you should have a deployed application on Heroku.
The source code for the gradle plugin and the default builpack can be found 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 an apiKey and some very simple settings to get our app singing sweetly in the clouds. Deploying the application on Heroku has never been this easy! The original article is here . Thanks for attention.

Source: https://habr.com/ru/post/220389/


All Articles