Info

Heroku が Javaをサポートしたということで試してみた。

1. プロジェクト作成

$ mvn archetype:create -DgroupId=com.heroku.javanese -DartifactId=Javanese
$ cd Javanese
$ mvn eclipse:eclipse

eclipseにインポートして covert to maven project

2. HelloWorld.java作成

package com.heroku.javanese;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;

public class HelloWorld extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().print("Hello from Java!\n");
    }

    public static void main(String[] args) throws Exception{
        Server server = new Server(Integer.valueOf(System.getenv("PORT")));
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        context.addServlet(new ServletHolder(new HelloWorld()),"/*");
        server.start();
        server.join();  
    }
}

3. pomファイル修正

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.heroku.javanese</groupId>
	<artifactId>Javanese</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Javanese</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-servlet</artifactId>
			<version>7.4.5.v20110725</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<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>
							<generateRepository>false</generateRepository>
							<programs>
								<program>
									<mainClass>com.heroku.javanese.HelloWorld</mainClass>
									<name>webapp</name>
								</program>
							</programs>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

4. .gitignore ファイル作成

$ echo "target" > .gitignore

5. いろいろ設定

$ mvn install

プロジェクトルートに Procfile ファイル作成

web: sh target/bin/webapp

環境変数 REPO に .m2/repository 設定

$ export REPO=$HOME/.m2/repository

foreman インストール、起動

$ sudo gem install foreman
$ foreman start

6. ローカルアクセスしてみる。
http://localhost:5000/

7. heroku に deploy

$ git init
$ git add .
$ git commit -m "init"
$ heroku create --stack cedar
$ git push heroku master

# heroku.com でプロジェクト名変更
$ git remote rm heroku
$ git remote add heroku git@heroku.com:javanese.git
$ git push heroku master

8. できた〜
http://javanese.heroku.com

Comments

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS