お題
GAE(Google App Engine)を理解するために、実際に自分でアプリ作って乗せてみる。
前回はGAEの構造や特徴について簡単にまとめた。
今回は、Java8で、App Engine用にあらかじめ用意されたMavenアーキタイプでプロジェクトを作ってGAEにデプロイするところまで。
開発環境
# OS
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"
# Java
$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
# Maven
$ mvn --version
Apache Maven 3.5.0
Maven home: /usr/share/maven
Java version: 1.8.0_181, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "linux", version: "4.13.0-46-generic", arch: "amd64", family: "unix"
前提
- スタンダード環境での開発とする。
- GCPプロジェクトは作成済み。
実践
01) App Engine用のSDKを取得
以降、下記に従って使えるようセッティング
https://cloud.google.com/appengine/docs/standard/java/download?hl=ja
02) Mavenプロジェクト作成
適当なGitHubリポジトリを作る
ローカルに取得した上記リポジトリの中にMavenプロジェクトを作る
$ cd java-webapi-for-gae-study/
$ mvn archetype:generate -Dappengine-version=1.9.54 -Djava8=true -Dapplication-id=【プロジェクトID】 -Dfilter=com.google.appengine.archetypes:
[INFO] Scanning for projects...
Choose archetype:
1: remote -> com.google.appengine.archetypes:appengine-flexible-archetype (A basic Java application with Google App Engine flexible.)
2: remote -> com.google.appengine.archetypes:appengine-skeleton-archetype (A skeleton application with Google App Engine)
3: remote -> com.google.appengine.archetypes:appengine-standard-archetype (A basic Java application with Google App Engine Standard)
4: remote -> com.google.appengine.archetypes:endpoints-skeleton-archetype (A skeleton project using Cloud Endpoints Frameworks with Google App Engine Standard)
5: remote -> com.google.appengine.archetypes:guestbook-archetype (A guestbook application with Google App Engine)
6: remote -> com.google.appengine.archetypes:hello-endpoints-archetype (A simple starter application using Cloud Endpoints Frameworks with Google App Engine Standard)
7: remote -> com.google.appengine.archetypes:skeleton-archetype (Archetype with a README about Google App Engine archetypes)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 3
Choose com.google.appengine.archetypes:appengine-standard-archetype version:
1: 0.1.0
2: 0.1.1
3: 0.2.0
4: 0.3.0
5: 0.4.0
6: 1.0.0
7: 1.0.1
8: 1.0.2
Choose a number: 8:
Define value for property 'groupId': com.example.sky0621
Define value for property 'artifactId': sky0621
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.example.sky0621: :
Confirm properties configuration:
groupId: com.example.sky0621
artifactId: sky0621
version: 1.0-SNAPSHOT
package: com.example.sky0621
CloudSDK_Tooling: true
appengine-version: 1.9.54
application-id: 【プロジェクトID】
java8: true
service: default
useAppEngineApi: false
useEndpoints1: false
useEndpoints2: false
useJstl: true
useObjectify: false
Y: : Y
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:05 min
[INFO] Finished at: 2018-10-02T01:24:06+09:00
[INFO] Final Memory: 19M/193M
[INFO] ------------------------------------------------------------------------
※Mavenについては下記参照
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
※Mavenを使ってのGAE用Javaプロジェクト作成については下記参照
https://cloud.google.com/appengine/docs/standard/java/tools/maven?authuser=1&hl=ja
03) ローカル環境でビルド・動作確認
ビルド
$ mvn clean install
動作確認
$ mvn appengine:run
===============================================
「http://localhost:8080」
===============================================
04) GAEにデプロイ
デプロイ
$ mvn appengine:deploy
[INFO] Scanning for projects...
[WARNING] The project com.example.sky0621:sky0621:war:1.0-SNAPSHOT uses prerequisites which is only intended for maven-plugin projects but not for non maven-plugin projects. For such purposes you should use the maven-enforcer-plugin. See https://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sky0621 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
〜省略〜
[INFO] GCLOUD: Services to deploy:
[INFO] GCLOUD:
[INFO] GCLOUD: descriptor: [/work/src/java/github.com/sky0621/java-webapi-for-gae-study/sky0621/target/appengine-staging/app.yaml]
[INFO] GCLOUD: source: [/work/src/java/github.com/sky0621/java-webapi-for-gae-study/sky0621/target/appengine-staging]
[INFO] GCLOUD: target project: [【プロジェクトID】]
[INFO] GCLOUD: target service: [default]
[INFO] GCLOUD: target version: [20181002t014521]
[INFO] GCLOUD: target url: [https://【プロジェクトID】.appspot.com]
[INFO] GCLOUD:
[INFO] GCLOUD:
[INFO] GCLOUD: Beginning deployment of service [default]...
[INFO] GCLOUD: #============================================================#
[INFO] GCLOUD: #= Uploading 18 files to Google Cloud Storage =#
[INFO] GCLOUD: #============================================================#
[INFO] GCLOUD: File upload done.
[INFO] GCLOUD: Updating service [default]...
[INFO] GCLOUD: .done.
[INFO] GCLOUD: Setting traffic split for service [default]...
[INFO] GCLOUD: ........done.
[INFO] GCLOUD: Deployed service [default] to [https://【プロジェクトID】.appspot.com]
[INFO] GCLOUD:
[INFO] GCLOUD: You can stream logs from the command line by running:
[INFO] GCLOUD: $ gcloud app logs tail -s default
[INFO] GCLOUD:
[INFO] GCLOUD: To view your application in the web browser run:
[INFO] GCLOUD: $ gcloud app browse
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:04 min
[INFO] Finished at: 2018-10-02T01:46:13+09:00
[INFO] Final Memory: 22M/269M
[INFO] ------------------------------------------------------------------------
===============================================
「http://【プロジェクトID】.appspot.com」
===============================================
===============================================
「https://【プロジェクトID】.appspot.com」
===============================================
GCPコンソールで確認
自動生成プロジェクトの中身
以下になる。これら全て自動生成によるもの。便利。
ついでに言うと、tree
コマンドも便利。
$ tree
.
├── README.md
└── sky0621
├── README.md
├── nbactions.xml
├── pom.xml
├── sky0621.iml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── sky0621
│ │ │ └── HelloAppEngine.java
│ │ └── webapp
│ │ ├── WEB-INF
│ │ │ ├── appengine-web.xml
│ │ │ ├── logging.properties
│ │ │ └── web.xml
│ │ └── index.jsp
│ └── test
│ └── java
│ └── com
│ └── example
│ └── sky0621
│ └── HelloAppEngineTest.java
└── target
├── appengine-staging
│ ├── WEB-INF
│ │ ├── appengine-generated
│ │ │ └── app.yaml
│ │ ├── appengine-web.xml
│ │ ├── classes
│ │ │ ├── com
│ │ │ │ └── example
│ │ │ │ └── sky0621
│ │ │ │ └── HelloAppEngine.class
│ │ │ └── source-context.json
│ │ ├── lib
│ │ │ ├── _ah_compiled_jsps-0000.jar
│ │ │ ├── jstl-1.2.jar
│ │ │ ├── org.apache.taglibs.taglibs-standard-impl-1.2.5.jar
│ │ │ ├── org.apache.taglibs.taglibs-standard-spec-1.2.5.jar
│ │ │ ├── org.eclipse.jdt.core.compiler.ecj-4.4.2.jar
│ │ │ ├── org.eclipse.jetty.apache-jsp-9.3.18.v20170406-nolog.jar
│ │ │ ├── org.mortbay.jasper.apache-el-8.0.33.jar
│ │ │ └── org.mortbay.jasper.apache-jsp-8.0.33.jar
│ │ ├── logging.properties
│ │ ├── min-quickstart-web.xml
│ │ ├── quickstart-web.xml
│ │ └── web.xml
│ ├── __static__
│ ├── app.yaml
│ └── index.jsp
├── generated-sources
│ └── annotations
├── generated-test-sources
│ └── test-annotations
├── maven-archiver
│ └── pom.properties
├── maven-status
│ └── maven-compiler-plugin
│ ├── compile
│ │ └── default-compile
│ │ ├── createdFiles.lst
│ │ └── inputFiles.lst
│ └── testCompile
│ └── default-testCompile
│ ├── createdFiles.lst
│ └── inputFiles.lst
├── sky0621-1.0-SNAPSHOT
│ ├── META-INF
│ ├── WEB-INF
│ │ ├── appengine-web.xml
│ │ ├── classes
│ │ │ └── com
│ │ │ └── example
│ │ │ └── sky0621
│ │ │ └── HelloAppEngine.class
│ │ ├── lib
│ │ │ ├── appengine-api-1.0-sdk-1.9.54.jar
│ │ │ ├── jstl-1.2.jar
│ │ │ ├── org.apache.taglibs.taglibs-standard-impl-1.2.5.jar
│ │ │ └── org.apache.taglibs.taglibs-standard-spec-1.2.5.jar
│ │ ├── logging.properties
│ │ └── web.xml
│ └── index.jsp
├── sky0621-1.0-SNAPSHOT.war
├── surefire-reports
│ ├── TEST-com.example.sky0621.HelloAppEngineTest.xml
│ └── com.example.sky0621.HelloAppEngineTest.txt
└── test-classes
└── com
└── example
└── sky0621
└── HelloAppEngineTest.class
48 directories, 47 files
まとめ
MavenとApp Engineプラグインを使うと、コマンド一発で https アクセス可能なドメインも用意された状態でWebアプリとしてデプロイされる。
とても楽。今後は、このプロジェクトにGAEの各種機能を試せるような味付けをしていく。