Tomcat Maven 外掛程式原型

Tomcat Maven 外掛程式有一個原型,可透過具體範例展示各種功能。

使用它

使用已發布的版本

mvn archetype:generate \
   -DarchetypeGroupId=org.apache.tomcat.maven \
   -DarchetypeArtifactId=tomcat-maven-archetype \
   -DarchetypeVersion=2.2

使用 SNAPSHOT 版本

mvn archetype:generate \
   -DarchetypeGroupId=org.apache.tomcat.maven \
   -DarchetypeArtifactId=tomcat-maven-archetype \
   -DarchetypeVersion=2.2 \
   -DarchetypeRepository=https://repository.apache.org/content/repositories/snapshots/

您將會看到以下輸出(我們將使用一個名為 tomcat-sample 的專案)

....
[INFO] Using property: groupId = org.apache.tomcat.maven
Define value for property 'artifactId': : tomcat-sample (project will be created in ./tomcat-sample )
...
cd tomcat-sample

專案詳細資料

注意:這是一個複雜的 hello world 範例 :-)

目標是公開稱為 HelloService 的 REST 服務,並在 Web 應用程式中使用它。

@Path( "HelloService" )
public interface HelloService
{
    @Path( "sayHello/{who}" )
    @GET
    @Produces( { MediaType.TEXT_PLAIN } )
    String sayHello( @PathParam( "who" ) String who );
}

Apache Cxf 將用於公開實作作為 REST 服務。

現在您有一個標準的多模組 Maven 專案配置

  • basic-api(服務介面)
  • basic-api-impl(服務預設實作)。有關 cxf 運作方式的更多詳細資料,請參閱 spring 設定檔。
  • basic-webapp(我們的 Webapp 模組)
  • basic-webapp-exec(用於產生可執行 War 的模組)
  • basic-webapp-it(用於執行 Selenium 測試的模組,並使用產生的 War)

使用專案中的外掛

執行 Web 應用程式

從頂層目錄,您可以使用:mvn tomcat6:run 或 mvn tomcat7:run(視您要使用的 Tomcat 版本而定)。

現在請開啟瀏覽器 https://127.0.0.1:9090,您將使用一個非常複雜的 hello world Web 應用程式範例

使用 Selenium 進行整合測試

使用 mvn clean install。預設瀏覽器為 Firefox,但您可以使用 -Pchrome 或 -Piexplore。

使用可執行 war/jar

現在您有一個可執行 jar/war。

試試看

cd basic-webapp-exec/target/
java -jar basic-webapp-exec-1.0-SNAPSHOT-war-exec.jar -httpPort 9191

然後使用瀏覽器前往 https://127.0.0.1:9191。

因此,您現在有一個 Tomcat 7 執行個體正在執行這個絕妙的應用程式,而無需安裝任何東西!