0
2

More than 3 years have passed since last update.

HerokuにGitでHelloWorldをデプロイする①[SpringBoot]

Last updated at Posted at 2021-08-29

環境

  • OS : Windows10
  • Git : 2.32.0
  • Heroku : 7.53.0
  • eclipse
  • Spring Boot(thymeleaf,Maven)

今回、GitHubと連携はしていません。

前提条件

  • Gitをインストール済み
  • Herokuに登録済み
  • Heroku CLIをインストール済み

Hello Worldのコードを準備する

image.png

GitHubで公開もしています。

Spring スターター・プロジェクトを作る

ファイル>新規>Spring スターター・プロジェクト
image.png

スターター・プロジェクトの設定

  • 名前 : HelloWorld
  • 型 : Maven
  • Havaバージョン : 11

image.png

依存関係の設定

  • Lombok
  • Thymeleaf
  • Spring Boot DevTools
  • Spring Web

image.png

Controllerクラスを作成

Controller.java
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;

public class Controller {
    @RequestMapping("/")
    public String index() {
        return "hello";
    }
}

index.html(view)を作成

index.htmlを作成するときは、src/main/resourcesのtemplatesの直下に作ってください。

index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>HELLO</title>
</head>
<body>
Hello World
</body>
</html>

pom.xml

pom.xmlは自動で作られます。
[依存関係の設定]で選択したものも入っていると思います。

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.demo</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorld</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Herokuに必要なファイルを作る

Herokuに必要なファイルを作ります。二つとも、プロジェクトの直下に作成してください。

  • system.properties : javaのバージョンを明記する
  • Procfile :Heroku上で実行するコマンドを定義する

eclipseでのファイルの作り方

image.png
プロジェクトの上で右クリック>新規>ファイル
※拡張子は必要ないです

system.properties

system.properties
#java.runtime.version=[javaのバージョン]
java.runtime.version=11

javaのバージョンを明記します。

Procfile

Procfileは「P」は大文字で、拡張子は付けないでください。

# web: java -Dserver.port=$PORT -jar target/任意の文字列-0.0.1-SNAPSHOT.jar
web: java -Dserver.port=$PORT -jar target/HelloWorld-0.0.1-SNAPSHOT.jar

今回はthymeleafを使ったwebアプリなので、webと書きます。
任意の文字列は、pom.xmlのartifactIdタグ、versionタグ内に記載されています。

pom.xml
    -- 略 --
    <groupId>com.example.demo</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorld</name>
    -- 略 --

Procfileを書かなくてもいいという記事もあったんですが、私はProcfileを書かないとうまく動きませんでした。

↓system.propertiesとProcfileについて、非常に参考になった記事です。
もっとsystem.propertiesとProcfileについて知りたい人は、ぜひ読んでみてください。
SpringBootで作成したアプリケーションをHerokuへデプロイ(公開)①

プロジェクトをエクスポートする

image.png
プロジェクト上で右クリック>エクスポート

image.png

ファイル・システムを選択

image.png

宛先ディレクトリーは好きな場所を選んでください。

次回は今回作ったHelloWorldをHerokuにデプロイします。
HerokuにGitでHelloWorldをデプロイする②[SpringBoot]

参考HP

【はじめてでも15分でできる】HerokuでJavaのWebアプリをサクッと動かす手順
SpringBootで作成したアプリケーションをHerokuへデプロイ(公開)①
Heroku初心者がJavaで作成したWebアプリをGitHub連携でデプロイする時つまずいた4つのこと

0
2
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
2