将多个war文件部署到jetty8中

时间:2011-09-08 08:34:38

标签: maven war maven-jetty-plugin

如何使用maven-jetty-plugin将多个webapps WAR文件部署到Jetty 8?

<contextHandlers>
  <contextHandler implementation="org.mortbay.jetty.webapp.WebAppContext">
    <war>${basedir}/dir/mywar.war</war>
  <contextPath>/path</contextPath>
</contextHandler>

似乎只适用于较旧的插件版本。

1 个答案:

答案 0 :(得分:1)

使用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>

这将允许使用例如这样的Spring bean工厂进行JNDI资源查找:

<bean id="exampleDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:jdbc/ExampleDB"/>
    <property name="resourceRef" value="true"/>
  </bean>

请注意,C3P0和Oracle引用将引入表面上是Jetty服务器本地的依赖项,因此应与WAR一起放在<plugin><dependencies>部分中。它们不必属于主要依赖项。

所以现在你的Maven构建将包含一个嵌入式Jetty Web服务器,配置为与多个WAR一起使用,所有WAR都绑定到pom.xml版本,提供HTTP和HTTPS,并支持池化数据库连接。对于集成开发环境而言,这几乎是您所需的一切。