Fork me on GitHub

Programming Design Notes

Maven Setup Jetty Development Server

| Comments

在 Maven 使用 Jetty 作為 Web Server 是非常簡單,在 pom.xml 加入以下設定:

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.26</version>
    <configuration>
        <stopPort>9966</stopPort>
        <stopKey>stop</stopKey>
    </configuration>
</plugin>

以後只需要輸入 mvn jetty:run 就會啟動 Jetty Web Server,輸入 mvn jetty:stop 則關閉, 非常方便。啟動後打開 http://localhost:8080 就可以用了。


如果要在 Unit Test 測試 URL 或版面的 HTML 是否正確,可以參考以下的 pom.xml 設定:

(pom.xml) download
<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>pro.ctlok.com</groupId>
    <artifactId>jetty</artifactId>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>jetty</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <stopPort>9966</stopPort>
                    <stopKey>stop</stopKey>
                </configuration>
                <executions>
                    <!-- Before test class execute, start Jetty web server first -->
                    <execution>
                        <id>start-jetty</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <!-- After test, stop Jetty web server -->
                    <execution>
                        <id>stop-jetty</id>
                        <phase>test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

以上設定可以在測試前啟動 Jetty Web Server,測試完畢就會自動關閉。

(HelloServlet.java) download
package pro.ctlok.com.jetty;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

  private static final long serialVersionUID = 6496751289098922589L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      resp.getWriter().print("Hello World");
  }

}
(UrlTest.java) download
package pro.ctlok.com.jetty.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;

public class UrlTest {

  @Test
  public void test() throws IOException {
      URL url = new URL("http://localhost:8080/jetty/index");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      assert conn.getResponseCode() == 200;
      
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      
      try{
          assert rd.readLine().equals("Hello World");
      } finally {
          rd.close();
      }
  }
}
(web.xml) download
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>jetty</display-name>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <servlet>
        <display-name>IndexServlet</display-name>
        <servlet-name>IndexServlet</servlet-name>
        <servlet-class>pro.ctlok.com.jetty.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>IndexServlet</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>
</web-app>