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.
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
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.
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 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"
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
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