でHOT Deploy?

久々にJava Servletをさわっていて、ソースファイルの修正のたびにjettyを再起動するのが苦痛になったのでJettyのHOT Deployの設定をしました。
一応、classの再読み込み処理が走って修正したソースの内容が反映されたので問題ないと思いますが、classのローディング周りは詳しくないので自信が無いです。

JettyのHOT Deployは、Jettyの起動オプションで scanIntervalSeconds を 1以上にすると有効になります。
scanIntervalSeconds で指定した間隔でwebapp下のclassファイルに変更あった場合、contextの再読み込みを実施するみたいです。

使用したフレームワークは下記になります。今回、RESTfulのJSON形式でMySQLのデータを出力できればいいので、View層のフレームワークは除外しています。

mavenのpom.xmlは、下記になります。EclipseCheckStyle Pluginが不要な場合は、削除してください。

<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>com.example</artifactId>
    <packaging>war</packaging>
    <version>0.0.1</version>
    <name>com.example</name>

    <repositories>
        <repository>
            <id>repo1.maven.org</id>
            <name>Maven2 Repository</name>
            <url>http://repo1.maven.org/maven2</url>
        </repository>
        <repository>
            <id>maven.seasar.org</id>
            <name>The Seasar Foundation Maven2 Repository</name>
            <url>http://maven.seasar.org/maven2</url>
        </repository>
        <repository>
            <id>maven.snapshot.seasar.org</id>
            <name>The Seasar Foundation Maven2 Repository(snapshot)</name>
            <url>http://maven.seasar.org/maven2-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>maven.t2framework.org</id>
            <name>The T2 Project Maven2 Repository</name>
            <url>http://maven.t2framework.org/maven2</url>
        </repository>
        <repository>
            <id>maven-snapshot.t2framework.org</id>
            <url>http://maven.t2framework.org/maven2-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>repository.codehaus.org</id>
            <name>Codehaus Maven2 Repository</name>
            <url>http://repository.codehaus.org</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- T2FRAMEWORK DEPENDENCIES -->
        <dependency>
            <groupId>org.t2framework.web</groupId>
            <artifactId>t2</artifactId>
            <version>0.6.3-rc1</version>
        </dependency>

        <!-- LOGGING DEPENDENCIES -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>0.9.15</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.15</version>
        </dependency>

        <!-- DOMA DEPENDENCIES -->
        <dependency>
            <groupId>org.seasar.doma</groupId>
            <artifactId>doma</artifactId>
            <version>1.2.0</version>
            <type>jar</type>
        </dependency>

        <!-- MySQL DEPENDENCIES -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.12</version>
        </dependency>

        <!--  JUNIT DEPENDENCY FOR TESTING -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
            <scope>test</scope>
        </dependency>

        <!--  JETTY DEPENDENCIES FOR TESTING  -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>${jetty.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>target/apt_generated</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <filtering>true</filtering>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <filtering>false</filtering>
                <directory>src/test/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <argLine>-Xmx512M</argLine>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <encoding>UTF-8</encoding>
                        <excludes>
                            <exclude>**/*</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.0</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warSourceExcludes>
                            .*,.*/,target/,work/,pom.xml,WEB-INF/classes/**/*.class,WEB-INF/lib/
                        </warSourceExcludes>
                        <archive>
                            <manifest>
                                <mainClass>org.t2framework.executablewar.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/apt_generated</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>source-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>ant-compile</id>
                        <phase>compile</phase>
                        <configuration>
                            <tasks>
                                <property
                                    name="apt_generated"
                                    value="target/apt_generated" />
                                <delete
                                    dir="${apt_generated}"
                                    failonerror="false" />
                                <mkdir
                                    dir="${apt_generated}" />
                                <javac
                                    fork="yes"
                                    compiler="javac1.6"
                                    debug="on"
                                    encoding="UTF-8"
                                    classpathref="maven.compile.classpath"
                                    srcdir="src/main/java"
                                    destdir="target/classes">
                                    <compilerarg
                                        line="-s ${apt_generated}" />
                                </javac>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                    <connectors>
                        <connector
                            implementation="org.eclipse.jetty.server.bio.SocketConnector">
                            <port>8000</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <wtpversion>2.0</wtpversion>
                    <classpathContainers>
                        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                    </classpathContainers>
                    <buildcommands>
                        <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
                        <buildcommand>net.sf.eclipsecs.core.CheckstyleBuilder</buildcommand>
                    </buildcommands>
                    <projectnatures>
                        <projectnature>org.eclipse.jdt.core.javanature</projectnature>
                        <projectnature>net.sf.eclipsecs.core.CheckstyleNature</projectnature>
                    </projectnatures>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <jetty.version>7.0.1.v20091125</jetty.version>
    </properties>
</project>

mvn jetty:run でJettyを起動した後、ソースファイルを修正して保存すると自動的にclassの再読み込みが始まります。
今のところ問題ありませんが、今後View層のフレームワークを追加したりキャッシュを使い始めると問題が出てくるかもしれません。

参考