使用 pom.xml 中的以下代码段。这改编自 Jetty 服务器指令,虽然它适用于 Jetty7,但它可以很容易地适应更高版本。
pom.xml
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<!-- Jetty 7.3+ requires Maven 3+ -->
<!-- Keep with Jetty 7.6.0 to avoid startup delays from Servlet API 3.0 -->
<version>7.6.0.RC1</version>
<configuration>
<stopKey>STOP</stopKey>
<stopPort>8009</stopPort>
<scanIntervalSeconds>10</scanIntervalSeconds>
<!-- Provide some JNDI resources (optional) -->
<jettyEnvXml>src/test/resources/jetty-jndi-config.xml</jettyEnvXml>
<!-- Register this application as a context -->
<webAppConfig>
<contextPath>/example</contextPath>
</webAppConfig>
<!-- Allow resources on the test classpath to be available -->
<useTestClasspath>true</useTestClasspath>
<!-- Add in any supporting application contexts (use dependencies section) -->
<contextHandlers>
<!-- Supporting WAR (note the use of a property entry for version, and see the dependency later - also Jetty 7 uses org.eclipse...) -->
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>
${settings.localRepository}/org/example/supporting-war/${supporting-war.version}/supporting-war-${supporting-war.version}.war
</war>
<contextPath>/supporting-war</contextPath>
</contextHandler>
</contextHandlers>
<connectors>
<!-- Later versions of Jetty don't require the Connector to be specified -->
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
<!-- SSL for localhost support -->
<connector implementation="org.mortbay.jetty.security.SslSocketConnector">
<port>8443</port>
<maxIdleTime>60000</maxIdleTime>
<!-- Provide a local key store for serving up SSL certificates -->
<keystore>src/test/resources/jetty-ssl.keystore</keystore>
<!-- Pick any password you like -->
<password>jetty6</password>
<keyPassword>jetty6</keyPassword>
</connector>
</connectors>
</configuration>
<dependencies>
<!-- This ensures that WAR files are downloaded from the repo -->
<!-- Example supporting WAR -->
<dependency>
<groupId>org.example</groupId>
<artifactId>supporting-war</artifactId>
<version>${supporting-war.version}</version>
<scope>compile</scope>
<type>war</type>
</dependency>
</dependencies>
</plugin>
我将 SSL 和 JNDI 配置留在了那里,以防万一有人需要查看它们是如何配置的。显然,他们将需要支持文件。SSL 假定您已经创建了一个合适的密钥库,其中包含一个 SSL 证书,例如 localhost。JNDI配置文件如下:
码头-jndi-config.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="ExampleDB" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>java:jdbc/ExampleDB</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">oracle.jdbc.driver.OracleDriver</Set>
<Set name="jdbcUrl">jdbc:oracle:thin:@//host:port/schema</Set>
<Set name="user">user</Set>
<Set name="password">password</Set>
<!-- Configure a simple connection test with timeout for subsequent queries -->
<Set name="preferredTestQuery">select 1 from dual</Set>
<Set name="checkoutTimeout">5000</Set>
</New>
</Arg>
</New>
</Configure>
这将允许 JNDI 资源查找使用例如这样的 Spring bean 工厂:
<bean id="exampleDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jdbc/ExampleDB"/>
<property name="resourceRef" value="true"/>
</bean>
请注意,C3P0 和 Oracle 参考将引入表面上对您的 Jetty 服务器本地的依赖项,因此应<plugin><dependencies>
与 WAR 一起放置在该部分中。它们不必位于主要依赖项中。
因此,现在您的 Maven 构建将包含一个嵌入式 Jetty Web 服务器,配置为与多个 WAR 一起使用,所有这些都绑定到 pom.xml 版本,提供 HTTP 和 HTTPS,并以池化数据库连接为后盾。对于集成开发环境,这几乎是您开箱即用所需的一切。