eclipse + maven で gwt 開発

eclipse + maven(m2eclipse) + Google Plugin for Eclipse を使って GWT 開発をしようと思ったら、意外と面倒くさかったので設定のメモ。

まずは、普通に maven の war プロジェクトを作ります。

だいたいこんな感じになるはず。

SampleProject
-- pom.xml
`-- src |-- main | |-- java | |-- resources | `-- webapp | `-- WEB-INF | `-- web.xml `-- test |-- java `-- resources

んで、 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>com.example</groupId>
  <artifactId>SampleProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
      <version>2.0.3</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-servlet</artifactId>
      <version>2.0.3</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

これで gwt.xml を追加すれば mavengwtコンパイルが行えます。mvn jetty:run-war で起動も出来ます。(jetty:run はsrc/main/webappを見にいくので不可)

次に eclipse の設定ですが、こちらはまず上記のプロジェクトを m2eclipse で Enable Dependency Management を ON にします。
それから、次に実行メニューから一度 mvn package を実行します。(重要)

そのあと Project の Properties から Google-> Web Application と移動して This Project has a WAR directory を選択し、 War directory に target/SampleProject-0.0.1-SNAPSHOT を指定します。
Google-> Web Toolkit の Use Google Web Toolkit のチェックももちろん ON に。

これで、 eclipse でも maven による推移的な jar の解決を行いつつ、 Google Plugin for eclipse の恩恵も受けることが出来ます。
ただし、依存 jar を追加した場合には必ず mvn package を実行して target/SampleProject-0.0.1-SNAPSHOT/WEB-INF/lib 以下に依存 jar を配備する必要があります。でないと、Google Plugin for eclipse で Web Application を起動した時に jar をロード出来ずにエラーになります。

あと gwt-servlet への依存は、なぜか Windows では必要なのデスが linuxmaven で実行した時は不要でした。
(なぜか target 以下の WEB-INF/lib に gwt-servlet-2.0.3.jar が配備される)