1. Google Cloud SDK をインストールする
https://cloud.google.com/sdk/docs/install2. 認証 & プロジェクト選択
> gcloud init
3. Ktor アプリを作る
IntelliJ IDEA に Ktor plugin を入れて、New Project wizard から Ktor プロジェクトを作る4. AppEngine の設定を追加する
build.gradle.kts
...
plugins {
...
// ↓ 追加
id("com.google.cloud.tools.appengine") version "2.2.0"
// ↓ 追加
war
}
...
dependencies {
...
// ↓ 追加
implementation("io.ktor:ktor-server-servlet:$ktor_version")
// ↓ 追加
compileOnly("com.google.appengine:appengine:$appengine_version")
}
// ↓ 追加
appengine {
deploy {
projectId = "GCLOUD_CONFIG"
version = "GCLOUD_CONFIG"
}
}
// ↓ 追加
tasks.named("run") {
dependsOn(":appengineRun")
}
settings.gradle.kts
...
// https://stackoverflow.com/questions/48502220/how-to-configure-appengine-gradle-plugin-using-kotlin-dsl/48510049#48510049
// ↓ 追加
pluginManagement {
repositories {
gradlePluginPortal()
google()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.google.cloud.tools.appengine") {
useModule("com.google.cloud.tools:appengine-gradle-plugin:${requested.version}")
}
}
}
}
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- path to application.conf file, required -->
<!-- note that this file is always loaded as an absolute path from the classpath -->
<context-param>
<param-name>io.ktor.ktor.config</param-name>
<param-value>application.conf</param-value>
</context-param>
<servlet>
<display-name>KtorServlet</display-name>
<servlet-name>KtorServlet</servlet-name>
<servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>
<!-- required! -->
<async-supported>true</async-supported>
<!-- 100mb max file upload, optional -->
<multipart-config>
<max-file-size>304857600</max-file-size>
<max-request-size>304857600</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>KtorServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
src/main/webapp/WEB-INF/appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
</appengine-web-app>
5. ローカルで実行する
> ./gradlew appengineRun
// 止めるとき
> ./gradlew appengineStop
6. デプロイする
> ./gradlew appengineDeploy
参考
- Run a Kotlin Ktor app on App Engine standard environment (注 Groovy なのとちょっと内容が古いです。(2021/3/9現在))
- https://ktor.io/docs/war.html (web.xml の設定)
- https://ktor.io/docs/google-app-engine.html
- https://github.com/ktorio/ktor-documentation/tree/main/codeSnippets/snippets/google-appengine-standard