0
0

More than 3 years have passed since last update.

graldeのSpring BootアプリケーションをHerokuへdeployしてみる

Last updated at Posted at 2021-08-01

概要

Spring Boot(gradle)をHerokuへデプロイ時に意外と躓いたのでメモとして。
他にも困っている人の参考になればうれしいです。

環境

  • windows 10 Home
  • java 11
  • spring-boot 2.3.5
  • gradle 7.1.1 (gradlew)

目次

  1. 前準備
  2. デモアプリ作成
  3. デプロイ

前準備

①Herokuのアカウントを作成
②Heroku Cliをインストール

公式:https://devcenter.heroku.com/ja/articles/heroku-cli
参考:初心者向けherokuをデプロイしよう

デモアプリ作成


とりあえず動きが見たいので、helloを返すだけのデモアプリを作成

一応githubに置いてあります。
https://github.com/Takeuchi713/spring-boot-gradle-heroku

build.gradle
plugins {
    id 'org.springframework.boot' version '2.5.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.takeuchi'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

HelloController.java
@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public ResponseEntity<String> hello(){
        return new ResponseEntity<String>("hello", new HttpHeaders(), HttpStatus.OK);
    }
}

デプロイ


ログイン、herokuのapp作成、、gitの設定、deployの設定などすべてコンソールで行う。

色々設定ファイルを追加するがすべてプロジェクトのrootへ追加が必要。
参考⇓
heroku-project.png

Heroku ~ gitの初期設定

gitレポジトリ化
>git init

Herokuへログイン
>~\deploy-heroku-maven\heroku login

アプリの作成。名前をしていしないとherokuが適当に作ってくれる
>heroku create

アプリ名を変更 (still-harbo-16072はherokuが適当につけた名前)
>heroku apps:rename --app still-harbor-16072 spring-gradle-deploy

Herokuをremoteへ追加
>heroku git:remote -a spring-gradle-deploy

設定ファイルを追加

デプロイ時のJavaバージョンを指定する設定ファイルを作成
>echo java.runtime.version=11 > system.properties

buildパスとdeploy時のportを一致させるための設定ファイルを作成
>echo web: java -Dserver.port=$PORT $JAVA_OPTS build/libs/heroku-demo-gradle-0.0.1-SNAPSHOT.jar > Procfile

※heroku-demo-gradleは自分のアプリ名

デプロイ

全てコミット
>git add .
>git commit -am "first commit"

デプロイ
>git push heroku main

動作確認

hello-gradle.png

終わりに

とりあえず、最低限?の設定でHerokuへデプロイできることを確認。
次はpostgres込みのデプロイを試したい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0