首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Maven Jetty Plugin 完整配置指南:從零到自動化部署

     編輯:Java 相關知識

Jetty 7 起外掛改名為 jetty-maven-plugin,符合 Maven 2 慣例。只要專案遵循標準目錄結構(src/main/webapp、target/classes、web.xml),無需額外配置即可運行:

mvn jetty:run

預設啟動 8080 埠,支援熱部署(掃描變更自動重載),極大縮短開發迴路。

核心命令

命令用途
jetty:run直接部署爆炸目錄,支援熱掃描
jetty:run-war先打包 WAR 再部署,模擬生產環境
jetty:run-exploded部署爆炸 WAR,介於兩者之間
jetty:start/stop背景啟動/停止,適合整合測試

整合測試自動化(生命週期綁定)

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.10</version>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <stopKey>foo</stopKey>
    <stopPort>9999</stopPort>
  </configuration>
  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals><goal>run</goal></goals>
      <configuration>
        <scanIntervalSeconds>0</scanIntervalSeconds>
        <daemon>true</daemon>
      </configuration>
    </execution>
    <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
      <goals><goal>stop</goal></goals>
    </execution>
  </executions>
</plugin>

<daemon>true</daemon> 讓 Jetty 在背景執行,Maven 繼續跑測試;測試結束自動停止。

settings.xml 指定外掛群組

<pluginGroups>
  <pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

容器級配置重點

  • Connectors:自訂埠口、NIO、閒置逾時。預設 8080,可用 -Djetty.port=9999 覆蓋。
  • scanIntervalSeconds:>0 啟用熱部署掃描(秒)。
  • jettyConfig:外部 jetty.xml 進階配置。
  • userRealms / requestLog:安全域與 NCSA 存取日誌。
  • reload:manual(按 Enter 重載)或 automatic(依掃描間隔)。

WebApp 級配置

<webAppConfig>
  <contextPath>/myapp</contextPath>
  <tempDirectory>${project.build.directory}/work</tempDirectory>
  <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
  <overrideDescriptor>src/main/resources/override-web.xml</overrideDescriptor>
</webAppConfig>

支援多環境 override-web.xml(開發/測試/生產差異化配置)。

掃描目標擴充

<scanTargets>
  <scanTarget>src/main/resources</scanTarget>
</scanTargets>
<scanTargetPatterns>
  <scanTargetPattern>
    <directory>src/other</directory>
    <includes><include>**/*.xml</include></includes>
    <excludes><exclude>**/secret.xml</exclude></excludes>
  </scanTargetPattern>
</scanTargetPatterns>

系統屬性與日誌

  • <systemProperties> 在 POM 固化 JVM 參數(如 Commons Logging 實作)。
  • 日誌預設用內建 Logger;偵測到 SLF4J 實作則自動切換。JSP 2.0 需 jcl-over-slf4j;JSP 2.1 (JDK 1.5+) 無額外依賴。

善用 Jetty Plugin 可實現「存檔即測試」的極速開發體驗,並無縫銜接 CI/CD 流程。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved