Deploy the application created by Spring Boot to Heroku (public) ①

Introduction

The application created by SpringBoot worked as expected in the local environment, and after that I was just taking a break to upload it to the production environment and publish it, but I was addicted to it twice as much as I imagined ...

Now that we've finally made it public, let's take a look at what we've done to make it public on Heroku.

I hope this article will be helpful for anyone other than me who is having trouble deploying on Heroku.

environment

OS: macOS Catalina 10.15.6 JDK:14.0.1 Spring Boot 2.3.3 jquery 3.3.1-1 bootstrap 4.2.1 maven eclipse

About the created application

The author created a CRUD application with Spring MVC by referring to "[Introduction to not regret] Spring dismantling new book". The main functions implemented in the application are as follows.

・ Login / logout ・ New member registration / withdrawal ・ Update registration information ・ Content search Create screens using thymeleaf for template engine and Bootstrap for CSS framework

I will not touch on the created controller and html file in this article. You might be stumbling upon deploying to Heroku and wondering if you have to recreate it from scratch, but that's not necessary.

① Membership registration on Heroku / Installation of Heroku CLI

Please refer to the article below to register as a member of Heroku. https://blog.cloud-acct.com/posts/u-heroku-deploy-intro/

↓ You can register as a member below. https://signup.heroku.com/login

Then install the Heroku CLI. Be sure to install it because it is deployed by terminal operation. The following is for mac.

 brew tap heroku/brew && brew install heroku

After installing, please check the version for confirmation.

$ heroku --version
heroku/7.46.2 darwin-x64 node-v12.16.2

② Create "Procfile" and "system.properties"

Create a file called Procfile that defines the command to be executed on Heroku under the root directory. Without it, when deploying to Heroku, it says "code = H14 desc =" No web processes running "" An error has occurred and the application cannot be run.

In Procfile, "P" is uppercase and no extension is added.

The author cloned heroku's Java sample project with the following command, I rewrote the contents in the root directory and used it.

git clone https://github.com/heroku/java-getting-started.git

Procfile is this file in the figure below. Example "heroku Java sample project" スクリーンショット 2020-10-28 0.59.39.png

Write the contents of the Procfile as shown below

web: java -Dserver.port=$PORT -jar target/Arbitrary string-0.0.1-SNAPSHOT.jar

I will explain in order from the left.

Part 1 "web" This is a process type called "web". The Heroku application runs multiple virtual machines in "different ways", The "process type" specifies how to move this. The Procfile describes the process type settings line by line. Describe the name of the process type, insert:, and then describe the command to set each process type.

Be sure to set the process type "web". If not set, the virtual machine will not handle HTTP requests.

Part 2 "java" I forgot, but to the right of "web:" is the command "java -jar 〇〇.jar" when executing a jar file with java.

Reference: https://teratail.com/questions/91621

Part 3 "-D"

According to the official documentation "45. Heroku" Make SpringBoot "-D" argument available as a property accessible from Spring Environment instance

There seems to be a meaning ...

Part 4 "server.port = $ PORT" You are setting the server HTTP port. $ PORT contains the environment variables assigned by Heroku PaaS.

Part 5 "-jar target / arbitrary character string -0.0.1-SNAPSHOT.jar" As I will explain later, when you do a move build, "arbitrary character string -0.0.1-SNAPSHOT.jar" will be created in the "target" folder directly under the root directory. "Target / arbitrary character string -0.0.1-SNAPSHOT.jar" is "arbitrary character string -0.0.1-SNAPSHOT.jar in the" target "folder", isn't it?

Regarding the jar file name, this applies to the information contained in the version tag and artifactId tag of pom.xml. The "arbitrary character string" above is the character string described in the artifactId tag.

In the example below, "hoge-0.0.1-SNAPSHOT.jar" will be created. When creating the contents in Procfile, please describe the character string in the artifactId tag in any character string.

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.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>

    <!--The information in the bottom two lines is reflected as the file name-->
	<artifactId>hoge</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>hoge</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>14</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest</artifactId>
		</dependency>

Next, create "system.properties" as shown below.

This file specifies the java version. Since the author uses JDK 14.0.1, it is described as 14. Please rewrite according to your version.

system.properties


java.runtime.version=14

Store it directly under the "Procfile" and "system.properties" root directories.

root/
 ├system.properties
 ├Procfile
 ├mvnw
 ├mvnw.cmd
 ├pom.xml
 ├target/
 ├src/
 ├docs/
 └README.md

reference Official documentation (English): https://docs.spring.io/spring-boot/docs/0.0.10.BUILD-SNAPSHOT/reference/htmlsingle/ Official document (Japanese) "2.3. Heroku": https://spring.pleiades.io/spring-boot/docs/current/reference/html/deployment.html

About Profile: https://creepfablic.site/2019/06/15/heroku-procfile-matome/ About the "web": http://info-i.net/heroku-ps

③ "webapp-runner" and "maven-compiler-plugin" for dependencies

Add "webapp-runner" to pom.xml.

Webapp Runner is a launcher application that launches Tomcat internally. You will need it to run your application on Heroku.

pom.xml


<plugin>
       <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.heroku</groupId>
                                <artifactId>webapp-runner</artifactId>
                                <version>9.0.30.0</version>
                                <destFileName>webapp-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
            	</execution>
            </executions>
</plugin>

Add "maven-compiler-plugin" to pom.xml. In my environment, if I didn't add this, the following error code was output to the Heroku console at the time of deployment.

This seems to be an error that occurs when maven tries to work with a version of Java older than the JDK version of the project specified by "maven-compiler-plugin". There is a version mismatch.

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project app: Fatal error compiling: invalid target release: 11 -> [Help 1]

source and target are compile-time options, but within this tag you specify the version of the jdk.

By the way, "source" specifies the version of the source code to be accepted, and "target" specifies the jdk version of the class file created when compilation is completed. I set "source" and "target" to "1.8".

When I tried to set it to "14", errors continued and I could not find a solution ... If you set it to "1.8", it works without any problem for some reason, and there is room for further investigation.

pom.xml


<build>
	    <plugins>
			<plugin>
	            <groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

The following is a summary of the added dependencies.

pom.xml


<build>
	    <plugins>
			<plugin>
	            <groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.heroku</groupId>
                                <artifactId>webapp-runner</artifactId>
                                <version>9.0.30.0</version>
                                <destFileName>webapp-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
            	</execution>
            </executions>
        	</plugin>
		</plugins>
	</build>

Don't forget to update the project.

reference About "Web app Runner": https://codezine.jp/article/detail/8187 "Webapp Runner" version: https://mvnrepository.com/artifact/com.heroku/webapp-runner About "Webapp Runner" (official documentation): https://devcenter.heroku.com/articles/java-webapp-runner About "Failed to execute goal org.apache.maven.plugins: maven-compiler-plugin": https://python5.com/q/laifarhi About "Fatal error compiling: invalid target release: x.x": https://qiita.com/gishi_yama/items/67e4c12ae90ad34e652b About "Source Option X is not currently supported": https://qiita.com/opengl-8080/items/bb32732f9aa5cb3495d2 About "target" and "source": http://dodododo.jp/java/javase_6_docs_ja/technotes/tools/solaris/javac.html

Deploy the application created by Spring Boot to Heroku (public) Continue to ② ... ↓ ② https://qiita.com/hiroki1994/items/72a52f2139c1088c623b

Recommended Posts

Deploy the application created by Spring Boot to Heroku (public) ②
Deploy the application created by Spring Boot to Heroku (public) ①
Deploy the WEB application by Spring Boot to Tomcat server as WAR
Deploy Spring Boot applications to Heroku without using the Heroku CLI
[Java] Deploy the Spring Boot application to Azure App Service
Deploy the Spring Boot project to Tomcat on XAMPP
Introduction to Java development environment & Spring Boot application created with VS Code
The story of raising Spring Boot 1.5 series to 2.1 series
Deploy a Spring Boot application on Elastic Beanstalk
◆ Get API created by Spring Boot from React
[Spring Boot] How to refer to the property file
Sign in to a Spring Boot web application on the Microsoft ID platform
How to set environment variables in the properties file of Spring boot application
Spring Boot + Heroku Postgres
Steps to deploy to Heroku
Spring Boot 2.3 Application Availability
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
How to boot by environment with Spring Boot of Maven
A story that stumbled when deploying a web application created with Spring Boot to EC2
The story of raising Spring Boot from 1.5 series to 2.1 series part2
Deploy Vapor Project to Heroku
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Deploy your application to WildFly
Introduction to Spring Boot Part 1
Deploy your application to EDAS using the Cloud Toolkit Maven plugin
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
I want to control the default error message of Spring Boot
I was able to deploy the Docker + laravel + MySQL app to Heroku!
Spring Boot application development in Eclipse
Spring Boot application code review points
Spring Boot for the first time
Hot deploy with Spring Boot development
Inquiry application creation with Spring Boot
Implement Spring Boot application in Gradle
How to use ModelMapper (Spring boot)
Upgrade spring boot from 1.5 series to 2.0 series
Deploy Rails on Docker to heroku
Deploy SpringBoot application to AWS EC2
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
[Spring Boot] I investigated how to implement post-processing of the received request.
Examine the contents of the WAR file generated by the project created by Spring Initializr
Procedure to make the value of the property file visible in Spring Boot
Java beginner tried to make a simple web application using Spring Boot
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
I used Docker to solidify the template to be developed with spring boot.
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
[For internal use] For those assigned to the Spring Boot project (under construction)
Deploy a Node.js application to an ECS instance using the Cloud Toolkit
Processing at application startup with Spring Boot
[Introduction to Spring Boot] Form validation check
Deploy to heroku with Docker (Rails 6, MySQL)
How to publish an application on Heroku
Web application scheduled to be created (editing)
Start web application development with Spring Boot
Deploy to Heroku [Ruby on Rails] Beginner
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Launch Nginx + Spring Boot application with docker-compose