heroku(lift編)

Windows7+cygwinでherokuにliftを乗せる手順。

$ mvn -version
Apache Maven 3.0.3 (r1075438; 2011-03-01 02:31:09+0900)
Maven home: C:\Java\apache-maven-3.0.3
Java version: 1.6.0_26, vendor: Sun Microsystems Inc.
Java home: C:\Java32\jdk1.6.0_26\jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

liftプロジェクト作成

$ mvn archetype:generate -DarchetypeRepository=http://scala-tools.org/repo-releases -DarchetypeGroupId=net.liftweb -
DarchetypeArtifactId=lift-archetype-blank_2.9.1 -DarchetypeVersion=2.4-M5

ビルド・実行

$ cd {projectのディレクトリ}
$ mvn package
$ mvn jetty:run

http://localhost:8080
で動作確認

C-cで止める

とりあえずgitに登録

# sbtのファイルは使わないので消してしまう
$ rm -rf project

$ echo target > .gitignore
$ git init
$ git add .
$ git commit -m "init"

war形式ではなくappassembler-maven-pluginを使うように変更する

pom.xml

diff --git a/pom.xml b/pom.xml
index 3f0f32f..1061caa 100755
--- a/pom.xml
+++ b/pom.xml
@@ -8,7 +8,7 @@
   <groupId>sample</groupId>
   <artifactId>lift</artifactId>
   <version>1.0-SNAPSHOT</version>
-  <packaging>war</packaging>
+  <packaging>jar</packaging>
   <name>lift Project</name>
   <inceptionYear>2010</inceptionYear>
   <properties>
@@ -58,7 +58,6 @@
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>
-      <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>junit</groupId>
@@ -67,10 +66,9 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.mortbay.jetty</groupId>
-      <artifactId>jetty</artifactId>
-      <version>6.1.25</version>
-      <scope>test</scope>
+      <groupId>org.eclipse.jetty</groupId>
+      <artifactId>jetty-servlet</artifactId>
+      <version>7.4.5.v20110725</version>
     </dependency>
     <!-- for LiftConsole -->
     <dependency>
@@ -84,6 +82,11 @@
   <build>
     <sourceDirectory>src/main/scala</sourceDirectory>
     <testSourceDirectory>src/test/scala</testSourceDirectory>
+    <resources>
+      <resource>
+        <directory>src/main/webapp</directory>
+      </resource>
+    </resources>
     <plugins>
       <plugin>
         <groupId>org.scala-tools</groupId>
@@ -117,6 +120,9 @@
               <goal>copy-resources</goal>
             </goals>
             <configuration>
+              <configuration>
+                <encoding>UTF-8</encoding>
+              </configuration>
               <overwrite>true</overwrite>
               <outputDirectory>${project.build.directory}</outputDirectory>
               <resources>
@@ -133,15 +139,6 @@
         </executions>
       </plugin>
       <plugin>
-        <groupId>org.mortbay.jetty</groupId>
-        <artifactId>maven-jetty-plugin</artifactId>
-        <version>6.1.25</version>
-        <configuration>
-          <contextPath>/</contextPath>
-          <scanIntervalSeconds>5</scanIntervalSeconds>
-        </configuration>
-      </plugin>
-      <plugin>
         <groupId>net.sf.alchim</groupId>
         <artifactId>yuicompressor-maven-plugin</artifactId>
         <version>0.7.1</version>
@@ -182,6 +179,26 @@
           </classpathContainers>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>appassembler-maven-plugin</artifactId>
+        <version>1.1.1</version>
+        <executions>
+          <execution>
+            <phase>package</phase>
+            <goals><goal>assemble</goal></goals>
+            <configuration>
+              <assembleDirectory>target</assembleDirectory>
+              <programs>
+                <program>
+                  <mainClass>RunWebApp</mainClass>
+                  <name>webapp</name>
+                </program>
+              </programs>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
     </plugins>
   </build>
   <reporting>

src/main/scala/RunWebApp.scala

import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.io.File
import org.eclipse.jetty.server.Server
import net.liftweb.http.LiftFilter
import org.eclipse.jetty.servlet.FilterHolder
import java.util.EnumSet
import org.eclipse.jetty.server.DispatcherType
import org.eclipse.jetty.servlet.DefaultServlet

object RunWebApp {

  def main(args: Array[String]): Unit = {
    val server = new Server(Integer.valueOf(System.getenv("PORT")));
    val context = new ServletContextHandler(ServletContextHandler.SESSIONS);

    context.setContextPath("/");
    server.setHandler(context);

    context.addServlet(new ServletHolder(new DefaultServlet), "/*");

    val tFilter = new LiftFilter()
      context.addFilter(new FilterHolder(new LiftFilter()), "/*", EnumSet.of(DispatcherType.REQUEST))

      server.start();
    server.join();
  }
}

ビルド

$ mvn package

実行

assebleしたjarを実行してみる

$ PORT=8080 ./target/bin/webapp

http://localhost:8080にアクセス。

C-cで止める

foreman

foreman化する

Procfile

web: sh target/bin/webapp
$ foreman start

http://localhost:5000にアクセス。

C-cで止める。

gitにコミット

$ git add Procfile
$ git add src/main/scala/RunWebApp.scala
$ git commit -a -m "fix for heroku"

herokuアプリ作成

$ heroku create --stack cedar

herokuデプロイ

$ git push heroku master

以上でheroku(lift編)終わり。