( ꒪⌓꒪) ゆるよろ日記

( ゚∀゚)o彡°オパーイ!オパーイ! ( ;゚皿゚)ノシΣ フィンギィィーーッ!!!

GAE/JでLift-1.1-Snapshotを動かす方法

GAE/JでLiftを動かす方法は、Liftコミッタでもあるscala勉強会東北の山中さんがまとめてくださってますが、英語なのでものっそい適当に意訳したものを掲載します。


元はこちらです。
http://groups.google.com/group/liftweb/browse_thread/thread/8193eb25e5a8b505


Liftは、version1.1からGAE/Jに対応しており、すでにmaven archetypeもあるので、mavenでプロジェクトを作成して簡単な設定を行うだけでdeployできます。


前提として、Google app engine のアカウントの取得とアプリケーションIDの発行が終わっており、
SDKもインストール済みのものとして進めます。

1. mavenでプロジェクトを作る。

mavenはインストール済みとして、コマンドラインで以下のように入力してプロジェクトを作成します。
最後の行の、-DgroupId および -DartifactId は、任意のものに変更してください。

$mvn archetype:create -U \
  -DarchetypeGroupId=net.liftweb \
  -DarchetypeArtifactId=lift-archetype-blank \
  -DarchetypeVersion=1.1-SNAPSHOT \
  -DremoteRepositories=http://scala-tools.org/repo-snapshots \
  -DgroupId=com.yuroyoro -DartifactId=lift-gae-test

これで、Liftアプリケーションのひな形ができました。

2. appengine-web.xmlを追加する。

GAE/Jでアプリケーションを動かすために必要な、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>liftropy</application>
  <version>1</version>
  <system-properties>
    <property name="in.gae.j" value="true" />
  </system-properties>
  <sessions-enabled>true</sessions-enabled>
  <static-files>
    <exclude path="/**" />
  </static-files>
</appengine-web-app>

applicationタグの内容は、アプリケーション名に変更してください。
また、static-filesタグが無い場合は、index.htmlなどはsinpetに処理されずに表示されてしまいます。

3. コンパイル

これでコンパイルが可能です。mvn package コマンドでコンパイルできます。

$ mvn package


コマンドを実行すると、プロジェクトディレクトリの配下にtargetディレクトリが作成されて、アプリケーションId名のディレクトリにコンパイルされたclassファイルなど、warにパッケージングされる内容が出力されているはずです。

4. ローカルでの動作確認

Google app engine SDKに付属のdev_appserver.shで、mavenでpackageされたディレクトリを指定して起動することで、ローカル環境での動作確認ができます。
dev_appserver.shは、SDKのインストールディレクトリ/binに配置されています。


Google app engine Eclipse pluginをインストールしている場合は、Eclipseのインストールディレクトリ/pluginに、Google app engine Eclipse pluginのフォルダ(com.google.appengine.eclipse.sdkbundle_1.2.1.v200905131143など)があるはずで、その中にappengine-java-sdk-1.2.1/bin/dev_appserver.shがあります。

$ [SDKのディレクトリ]/dev_appserver.sh target/liftropy-1.0-SNAPSHOT


サーバーが起動した後、http://localhost:8080/index.htmlにアクセスすると、画面が表示されるはずです。

5. デプロイ

Google app engine SDKに付属のappcfg.shで、コンパイルしたアプリケーションをデプロイできます。
もちろん、デプロイの前に、アプリケーション名を登録しておく必要があります。

$ [SDKのディレクトリ]/appcfg.sh update target/liftropy-1.0-SNAPSHOT

6. DataStoreを使う方法(JPA編)

上記の方法では、GAE/JでのDataStoreを利用することができません。


しかしっ!


山中さんがJPAを利用してDataStoreを使う方法で、実際にdemoをGAE/Jにデプロイしています。


demo http://lift-example.appspot.com/jpademo/
source http://github.com/ymnk/lift-gae-jpa/tree/master


方法は以下の通りです。

6-1. pom.xmlを編集する

以下の内容をpom.xmlに追加します。
ぶっちゃけ、ここのpom.xmlを参考にしてください。

内にDataNucleusのリポジトリを追加。

    <repository>
      <id>DataNucleus_Repos</id>
      <name>DataNucleus Repository</name>
      <url>http://www.datanucleus.org/downloads/maven</url>
      <layout>legacy</layout>
    </repository>
    <repository>
      <id>DataNucleus_Repos2</id>
      <name>DataNucleus Repository</name>
      <url>http://www.datanucleus.org/downloads/maven2</url>
    </repository>

内にDataNucleusのリポジトリを追加。

    <pluginRepository>
      <id>DataNucleus_2</id>
      <url>http://www.datanucleus.org/downloads/maven2/</url>
    </pluginRepository>


内にAppEngine関連のjarを追加。

    <!--app engine related dependencies-->
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-1.0-sdk</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>geronimo-jta</artifactId>
      <version>1.1_spec-1.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>jdo2-api</artifactId>
      <version>2.3-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>geronimo-jpa</artifactId>
      <version>3.0_spec-1.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.appengine.orm</groupId>
      <artifactId>datanucleus-appengine</artifactId>
      <version>1.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.datanucleus</groupId>
      <artifactId>datanucleus-core</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>datanucleus-jpa</artifactId>
      <version>1.1.0</version>
    </dependency>

    <dependency>
      <groupId>net.liftweb</groupId>
      <artifactId>lift-jpa</artifactId>
      <version>1.1-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.3.2.GA</version>
      <exclusions>
    <exclusion>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
    </exclusion>
        <exclusion>
          <groupId>asm</groupId>
          <artifactId>asm</artifactId>
        </exclusion>
        <exclusion>
          <groupId>asm-attrs</groupId>
          <artifactId>asm-attrs</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

内に、DataNucleusでエンハンスするためにbuild.xmlでantを走らせるように追加。

      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webappDirectory>${basedir}/war</webappDirectory>
        </configuration>
      </plugin>

      <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-antrun-plugin</artifactId>
       <executions>
         <execution>
           <id>package</id>
           <phase>package</phase>
           <configuration>
             <tasks>
              <ant antfile="${basedir}/build.xml">
                 <target name="datanucleusenhance"/>
              </ant>
             </tasks>
           </configuration>
           <goals>
             <goal>run</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
6-2. build.xmlを作成・編集する

ここからbuild.xmlをダウンロードして、プロジェクト直下に配置します。

で、2行目のlocation="????"となっている部分をGAEのSDKのディレクトリに変更します。

<property name="sdk.dir" location="[SDKのディレクトリ]"/>
6-3. GAE/J SDKのDataNucleus Enhancerを最新のものに変更する

GAE/J SDK1.2.1に付属しているDataNucleus Enhancer 1.1.1では、scalaのクラスファイルをエンハンスすることができません。
Enhancerのバージョンを1.1.3にあげれば対応できます。


DataNucleusのサイトから、datanucleus-enhancerの最新のjarをダウンロードして、[SDKのディレクトリ]/lib/tools/orm/に配置します。
同時に、[SDKのディレクトリ]/lib/tools/orm/にある古いバージョンのdatanucleus-enhancer-1.1.1.jarを削除します。

6-4. コンパイル

以上で準備は完了です。
これで、mvn packageで普通にコンパイルできるはずです。

実際にJPAを利用してDataStoreを操作する方法は、こちらのソースを参考にすると良いでしょう。
ってか、書く気力がなくなった。。。


ちなみに、JDO版も現在開発中です。