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 繼續跑測試;測試結束自動停止。
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
-Djetty.port=9999 覆蓋。jetty.xml 進階配置。manual(按 Enter 重載)或 automatic(依掃描間隔)。<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 實作)。jcl-over-slf4j;JSP 2.1 (JDK 1.5+) 無額外依賴。善用 Jetty Plugin 可實現「存檔即測試」的極速開發體驗,並無縫銜接 CI/CD 流程。