slim3-archetype-quickstartを使ってNetBeans用のslim3Mavenプロジェクトを作ってみた

slim3-archetype-quickstartと、maven-gae-pluginを使ってNetBeansslim3Mavenプロジェクトを構築してみました。
この手順でできるのは、アーキタイプで作成されたプロジェクトをローカルで動かして動作確認するところまでです。デプロイはまた後日。

2010/4/30追記
つぎのようなデメリットがあることに気がつきましたorz。ばりばり作っていきたい場合はeclipseで開発した方がいいかも。

  • HOTリローディングできない。(確認したわけじゃないですが、maven使ってるのでおそらくムリでしょう。)
  • controllerやmodel、serviceを手動で作成しなければいけない。(slim3が提供しているblankプロジェクトの場合は、antスクリプトでやってくれます。)

1.slim3-archetype-quickstartでプロジェクトを作成する。

archetype-pluginを使用するの手順1から手順5までを実行してください。

1.コマンドラインから以下を実行します
mvn archetype:generate -DarchetypeCatalog=http://slim3.googlecode.com/svn/trunk/repository
2.どのプロジェクトを作成するかを求められるので、slm3-archetype-quickstart を意味する "1" を入力します。注意)現在は選択肢が(1)以外ありません。
3.続けてgroupId, artifactId, version, packageの入力を求められるので適宜入力します。
4.最後に、入力した値の確認を求められるので、問題なければ "Y" を入力します。
5.maven用のslim3プロジェクトが作成されます。

2.NetBeansで作成したプロジェクトを開く。

eclipse:eclipseゴールを実行せずに作成したプロジェクトを開きます。

3.pom.xmlを編集してeclipse用の設定を削除する。

3.1.プロジェクトに名前を付ける

mavenのプロジェクト名が設定されていないので設定します。

<project>
    ...
    <name>slim3example</name>
    ...
</project>
3.2.maven-eclipse-pluginの記述を削除する。

eclipseと連携する必要がないので、maven-eclipse-pluginの記述を削除します。

3.3.maven-dependency-pluginの記述を削除する。

mavenで実行する分には、依存ライブラリなどのコピーは不用なので、こちらも同様に削除します。
不用になったeclipse.libプロパティも削除します。

3.4.maven-clean-pluginの記述を以下のように変更する。

slim3のテスト実行時にbuild以下に残ったファイルを削除するため以下のように変更します。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>build</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
3.5.utf-8の箇所をプロパティに置換する。

utf-8エンコーディング設定が複数箇所にあるので、以下のようなプロパティを参照するように置き換えます。

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

の二箇所の「utf-8」を「${project.build.sourceEncoding}」に置換します。

3.6.generated.srcプロパティをtarget/apt_generatedに変更する。

cleanゴール実行時に削除されるようにするため、「.apt_generated」を「target/apt_generated」に変更します。

<property>
    <generated.src>target/apt_generated</generated.src>
</property>

4.pom.xmlを編集してmaven-gae-plugin用の設定を追加する。

4.1.repositories、pluginRepositoriesにmaven-gae-pluginのレポジトリを追加する。

slim3のレポジトリ設定にmaven-gae-plugin用のレポジトリ設定を付け加えると以下のようになります。

    <repositories>
        <repository>
            <id>maven.seasar.org</id>
            <name>The Seasar Foundation Maven2 Repository</name>
            <url>dav:https://www.seasar.org/maven/maven2</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </repository>
        <repository>
            <id>maven-gae-plugin-repo</id>
            <name>maven-gae-plugin repository</name>
            <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>maven-gae-plugin-repo</id>
            <name>maven-gae-plugin repository</name>
            <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>
        </pluginRepository>
    </pluginRepositories>
4.2.maven-gae-pluginの記述を追加する。

最後のほうで実行されるプラグインなので、maven-clean-pluginの前に追加しておくとわかりやすいかもしれません。

            <plugin>
                <groupId>net.kindleit</groupId>
                <artifactId>maven-gae-plugin</artifactId>
                <version>0.5.7</version>
                <configuration>
                    <unpackVersion>${gae.version}</unpackVersion>
                </configuration>
            </plugin>
4.3.maven-gae-plugin用のプロパティを設定する。
  • gae.homeを追加。
  • pom.xml内のappengine.versionをgae.versionに置換。

slim3で使っているappengine.versionのプロパティ名を変更したのは、maven-gae-pluginの設定にあわせてたためです。

この時点でのプロパティはつぎのようになってます。

    <properties>
        <slim3.version>1.0.3</slim3.version>
        <gae.version>1.3.3.1</gae.version>
        <gae.home>
            ${settings.localRepository}/com/google/appengine/appengine-java-sdk/${gae.version}/appengine-java-sdk-${gae.version}
        </gae.home>
        <generated.src>target/apt_generated</generated.src>
        <generated.war>war</generated.war>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

${appengine.version}を${gae.version}に置換したのは次の箇所です。

  • appengine-api-stubs
  • appengine-local-runtime
  • appengine-testing
4.4.maven-war-pluginを追加する。
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1-alpha-2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/appengine-web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
4.5.warディレクトリを「webapp」という名前に変更し、src/mainディレクトリに移動する。

maven-war-pluginのデフォルトのプロジェクト構成では、src/main/webappがwar用のファイルを格納するディレクトリになるためです。
NetBeansのファイルビューでは操作できなかったため、Explorerから変更しました。

変更後のディレクトリ構造は次の通りです。

    pom.xml
    src
    +---main
    |   +---java
    |   |   +---tutorial
    |   |       +---slim3example
    |   |           +---controller
    |   |           |   +---IndexController.java
    |   |           |
    |   |           +---model
    |   |           |   +---Slim3Model.java
    |   |           |
    |   |           +---service
    |   |               +---Slim3Service.java
    |   |
    |   +---resources
    |   |   +---logging.properties
    |   |
    |   +---webapp
    |       +---WEB-INF
    |           +---appengine-web.xml
    |           +---queue.xml
    |           +---web.xml
    |
    +---test
        +---java
            +---tutorial
                +---slim3example
                    +---controller
                    |   +---IndexControllerTest.java
                    |
                    +---service
                        +---Slim3ServiceTest.java

4.6.buildのoutputdirectory設定を削除する。

warディレクトリに出力する必要がなくなり、またmavenのデフォルト設定で問題なかったので削除しました。

        <outputDirectory>${generated.war}/WEB-INF/classes</outputDirectory>

ここまでの手順を適用したpom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>tutorial</groupId>
    <artifactId>slim3example</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>slim3example</name>
    <properties>
        <slim3.version>1.0.3</slim3.version>
        <gae.version>1.3.3.1</gae.version>
        <gae.home>
            ${settings.localRepository}/com/google/appengine/appengine-java-sdk/${gae.version}/appengine-java-sdk-${gae.version}
        </gae.home>
        <generated.src>target/apt_generated</generated.src>
        <generated.war>war</generated.war>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>maven.seasar.org</id>
            <name>The Seasar Foundation Maven2 Repository</name>
            <url>dav:https://www.seasar.org/maven/maven2</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </repository>
        <repository>
            <id>maven-gae-plugin-repo</id>
            <name>maven-gae-plugin repository</name>
            <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>maven-gae-plugin-repo</id>
            <name>maven-gae-plugin repository</name>
            <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.slim3</groupId>
            <artifactId>slim3</artifactId>
            <version>${slim3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slim3</groupId>
            <artifactId>slim3-gen</artifactId>
            <version>${slim3.version}</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-api-stubs</artifactId>
            <version>${gae.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-local-runtime</artifactId>
            <version>${gae.version}</version>
            <scope>test</scope>
        </dependency>
    <!--
    <dependency>
    <groupId>com.google.appengine</groupId>
    <artifactId>appengine-testing</artifactId>
    <version>${gae.version}</version>
    <scope>test</scope>
    </dependency>
    -->
    </dependencies>
    <build>
        <finalName>slim3-maven</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.4</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>apt-maven-plugin</artifactId>
                    <version>1.0-alpha-3</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.slim3</groupId>
                            <artifactId>slim3-gen</artifactId>
                            <version>${slim3.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${generated.src}</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <outputDirectory>${generated.src}</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1-alpha-2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/appengine-web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>net.kindleit</groupId>
                <artifactId>maven-gae-plugin</artifactId>
                <version>0.5.7</version>
                <configuration>
                    <unpackVersion>${gae.version}</unpackVersion>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>build</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

5.mavenでgae:unpackゴールを実行しgae-java-sdkを準備する。

gae-java-sdkをダウンロードしてきて、レポジトリ内に展開するというゴールです。これをやっとかないとgae:runが実行出来ません。
これはmaven-gae-pluginを最初に使う時か、gaeのバージョンを変更したら一回だけやっておけば良いです。

6.mavenでgae:runゴールを実行しローカルでテストする。

localhost:8080で起動します。Hello World!と時刻が表示されていれば成功です。
リロードすれば表示されている時刻が追加されていきます。

※テスト実行時に、buildディレクトリ以下にテスト用データストア(local_db.bin)が作成されるのですが、これを変更する方法はまだ分かりません。slim3のorg.slim3.tester.AppEngineTesterクラスが関係してそうなのですが...