Jason 这对我有用,这是我经常使用的 pom.xml(相关部分):
<dependencies>
<!-- Jetty dependencies -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-embedded</artifactId>
<version>6.1.26</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.0.2.v20100331</version>
<configuration>
<webAppConfig>
<contextPath>/jetty-example</contextPath>
<descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
</webAppConfig>
<scanIntervalSeconds>5</scanIntervalSeconds>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
这是位于上面在 webappconfig 中作为描述符指定的位置的 web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>HelloWorld Application</display-name>
<description>
lalala
</description>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.mypackage.jetty.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
和 servlet 本身:
public final class Hello extends HttpServlet {
private static final long serialVersionUID = 903359962771189189L;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head>");
writer.println("<title>Sample Application Servlet Page</title>");
writer.println("</head>");
writer.println("<body bgcolor=white>");
writer.println("<table border=\"0\" cellpadding=\"10\">");
writer.println("<tr>");
writer.println("<td>");
writer.println("</td>");
writer.println("<td>");
writer.println("<h1>W00w I totally work</h1>");
writer.println("</td>");
writer.println("</tr>");
writer.println("</table>");
writer.println("</body>");
writer.println("</html>");
}
}
您可以通过运行来运行服务器mvn jetty:run
并在以下位置检查它http://localhost:9080/jetty-example/hello
此外,您可以将执行添加到插件部分并在完成构建项目时启动码头。无需mvn jetty:run
每次手动操作。
<executions>
<execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> <daemon>true</daemon> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution>
</executions>
您还可以添加我用于数据库的码头配置文件(用于不同的环境)。webAppConfig
您可以像这样在您的码头插件中添加文件位置:
<webAppConfig>
<contextPath>/my-tool</contextPath>
<descriptor>${basedir}/src/main/webapp/WEB-INF/jetty/web.xml </descriptor>
<jettyEnvXml>${basedir}/src/main/webapp/WEB-INF/jetty/jetty-env.xml </jettyEnvXml>
</webAppConfig>
jetty-env.xml 的示例内容:
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"[]>
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<!-- PRIMARY DATABASE -->
<New id="devDS" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>primaryDS</Arg>
<Arg>
<!-- i.e. Postgress -->
<New class="org.postgresql.ds.PGSimpleDataSource">
<Set name="User">myuser</Set>
<Set name="Password">password</Set>
<Set name="DatabaseName">database</Set>
<Set name="ServerName">database.stackoverflow.com</Set>
<Set name="PortNumber">5432</Set>
</New>
</Arg>
</New>
<!-- BACKUP DATABASE
<New id="devDS" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>backupDS</Arg>
<Arg>
.....
</Arg>
-->
</Configure>
你应该对此很好。