GoogleAppEngine for Javaはありますが、JavaではなくScalaで書きたいので、Scalaでデプロイ出来ないかなと色々探してみました。
やること
やることは簡単です。
- sbt-appengine-plugin導入
- appengine-web.xmlの準備
- Servlet用のclassとweb.xmlの準備
- Google App Engine SDKのダウンロードとAPPENGINE_SDK_HOMEの設定
使ったもののVersionはScala 2.11.7, sbt 0.13.11 です。
1. sbt-appengine pluginの導入
こちらを利用します。
2ファイル用意する必要があります。
0.12.xの時は違う設定になるようなので注意です。
appengine.sbt
libraryDependencies += "org.mortbay.jetty" % "jetty" % "6.1.22" % "container" appengineSettings
/project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-appengine" % "0.6.2")
2. appengine-web.xmlの準備
/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"> <application>dark</application> <version>1</version> <threadsafe>true</threadsafe> </appengine-web-app>
ここでのapplicationはGoogle Console上の project id
を指定します。
3. Servlet用のclassとweb.xmlの準備
/src/main/scala/com/ru/waka/servlets/Main.scala
package com.ru.waka.servlets import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} class Main extends HttpServlet { override def doGet(request: HttpServletRequest, response: HttpServletResponse) { response.setContentType("text/plain") response.getWriter.println("Hello, world") } }
/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>Main</servlet-name> <servlet-class>com.ru.waka.servlets.Main</servlet-class> </servlet> <servlet-mapping> <servlet-name>Main</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
4. Google App Engine SDKのダウンロードとの設定
SDKをダウンロードして、APPENGINE_SDK_HOMEという環境変数を設定する必要があります。
ダウンロードはこちらで。
Download the Google App Engine SDK - App Engine — Google Cloud Platform
APPENGINE_SDK_HOMEをunzipした↑のディレクトリのパスに設定します。
export APPENGINE_SDK_HOME=/path/to/unzipped_google_app_engine_sdk
tips
実行
$sbt [info] Loading project definition from /path/to/scala-on-appengine/project [info] Set current project to scala-on-appengine (in build file:/path/to/scala-on-appengine/) # Local Server 起動 > appengineDevServer [info] Compiling 1 Scala source to /path/scala-on-appengine/target/scala-2.11/classes... [info] 'compiler-interface' not yet compiled for Scala 2.11.7. Compiling... [info] Compilation completed in 11.87 s [info] Packaging /path/scala-on-appengine/target/scala-2.11/scala-on-appengine_2.11-0.1-SNAPSHOT.war ... [info] Done packaging. [info] Starting dev server in the background ... objc[4157]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined. [success] Total time: 21 s, completed 2016/03/26 16:13:25 Listening for transport dt_socket at address: 1044 > 3 26, 2016 4:13:29 午後 com.google.apphosting.utils.jetty.JettyLogger info 情報: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger 3 26, 2016 4:13:29 午後 com.google.apphosting.utils.jetty.JettyLogger info 情報: jetty-6.1.x 3 26, 2016 4:13:31 午後 com.google.apphosting.utils.jetty.JettyLogger info 情報: Started SelectChannelConnector@127.0.0.1:8080 3 26, 2016 4:13:31 午後 com.google.appengine.tools.development.AbstractModule startup 情報: Module instance default is running at http://localhost:8080/ 3 26, 2016 4:13:31 午後 com.google.appengine.tools.development.AbstractModule startup 情報: The admin console is running at http://localhost:8080/_ah/admin 3 26, 2016 4:13:31 午後 com.google.appengine.tools.development.DevAppServerImpl doStart 情報: Dev App Server is now running # Deploy > appengineDeploy appcfg.sh should ideally be run using Java 7 (also known as 1.7). The java executable at /usr/bin/java reports: java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode) Running a more recent version of Java can lead to apps that are apparently correct but do not work when uploaded to App Engine. You can download JDK 7 from: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html Please enter code:
ここまで来るとこんな画面が出るので、ログインしてみましょう。
Please enter code:のところにモザイクがかかっている値を入れると次に進みます。
Please enter code: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Reading application configuration data... #appengine-web.xmlに記載している applicationの値(app_id) が存在していないので、 # エラーが出てデプロイされていませんが、合っていればデプロイされます。 Beginning interaction for module default... 3 26, 2016 4:31:18 午後 com.google.appengine.tools.admin.AbstractServerConnection send1 警告: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=dark&version=1& 403 Forbidden You do not have permission to modify this app (app_id=u'dark'). This is try #0 3 26, 2016 4:31:18 午後 com.google.appengine.tools.admin.AbstractServerConnection send1 警告: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=dark&version=1& 403 Forbidden You do not have permission to modify this app (app_id=u'dark'). This is try #1 3 26, 2016 4:31:19 午後 com.google.appengine.tools.admin.AbstractServerConnection send1 警告: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=dark&version=1& 403 Forbidden You do not have permission to modify this app (app_id=u'dark'). This is try #2 3 26, 2016 4:31:19 午後 com.google.appengine.tools.admin.AbstractServerConnection send1 警告: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=dark&version=1& 403 Forbidden You do not have permission to modify this app (app_id=u'dark'). This is try #3 com.google.appengine.tools.admin.HttpIoException: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=dark&version=1& 403 Forbidden You do not have permission to modify this app (app_id=u'dark'). Unable to update app: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=dark&version=1& 403 Forbidden You do not have permission to modify this app (app_id=u'dark'). Please see the logs [/var/folders/3x/tr8_68tx29v9skp0lv76ljvc0000gn/T/appcfg1764707419651306170.log] for further information.
というところでした。ScalaでもGoogle App Engineは使えました。
今回試したレポジトリはこちらです。
git cloneと 4. Google App Engine SDKのダウンロードとの設定
だけで動くと思います。
github.com