1. 程式人生 > >Jetty和Tomcat執行Maven Web專案幾種方法

Jetty和Tomcat執行Maven Web專案幾種方法

目錄

   1.採用Jetty執行Maven Web專案

   2.採用Tomcat執行Maven Web專案

 1、採用JettyTomcat執行Maven Web專案

  1)採用Jetty執行Maven Web專案

      第一步:安裝Jetty外掛到Eclipse上,首先Eclipse help->install new software 中 Location : http://run-jetty-run.googlecode.com/svn/trunk/updatesite/ 

把選項勾上,然後等待它  下載安裝,完成之後重啟 eclipse 即可。

    

  第二步:Maven專案POM.XMl  新增Jetty的外掛jetty-maven-plugin,我們要執行test-maven-console專案,所以要在這個專案POM.XML中新增

  1. <plugins>  
  2.         <plugin>  
  3.             <groupId>org.apache.maven.plugins</groupId>  
  4.             <artifactId>maven-compiler-plugin</artifactId>  
  5.             <configuration>  
  6.                 <source>${version.jdk}</source>  
  7.                 <target>${version.jdk}</target>  
  8.                 <showWarnings>true</showWarnings>  
  9.                 <compilerArguments>  
  10.                     <verbose />  
  11.                     <bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>  
  12.                 </compilerArguments>                    
  13.             </configuration>  
  14.             </plugin>  
  15.          <plugin><!-- clean  -Djetty.port=9090 jetty:run -->  
  16.          <groupId>org.mortbay.jetty</groupId>  
  17.          <artifactId>jetty-maven-plugin</artifactId>  
  18.          <configuration>  
  19.            <reload>automatic</reload>  
  20.            <scanIntervalSeconds>10</scanIntervalSeconds>  
  21.            <systemProperties>  
  22.            </systemProperties>  
  23.            <useTestClasspath>true</useTestClasspath>  
  24.            <webAppConfig>  
  25.              <contextPath>/</contextPath>  
  26.            </webAppConfig>  
  27.          </configuration>  
  28.        </plugin><pre name="code"class="java" style="font-size: 18px;"></plugins>  
說明:  maven-compiler-plugin這個外掛是編譯外掛

第三步:配置執行埠等。點選test-maven-console專案右擊Run As -->Run Configurations  然後點選Jetty Webapp右擊New 配置Jetty版本和Port埠(

不至於埠衝突

      

點選RUN執行看後臺Console有沒有報錯,如果沒報錯就代表執行成功

   

我們也可以以DUG形式啟動,方便我們對專案進行跟蹤

2.採用Tomcat執行Maven Web專案

  第一步:Eclipse這邊不需要引入Tomcat外掛,直接在要執行的專案中引入Tomcat外掛就可以了,test-maven-console專案新增Tomcat的外掛tomcat6-maven-plugin (到2.0版本tomcat-maven-plugin現在已拆分成tomcat7-maven-plugintomcat6-maven-plugin了,而groupId也由org.codehaus.mojo改為org.apache.tomcat.maven。)我們這邊使用的是tomcat6-maven-plugin 

    test-maven-console的POM.XML中加入

  1.     <!-- tomcat執行 clean tomcat6:run -->  
  2.         <plugin>  
  3.         <groupId>org.apache.tomcat.maven</groupId>  
  4.             <artifactId>tomcat6-maven-plugin</artifactId>  
  5.             <version>2.2</version>  
  6.         <configuration>  
  7.     <!-- http port -->  
  8.                 <port>9090</port>  
  9.                 <!-- application path always starts with /-->  
  10.                 <path>/</path>  
  11.                 <uriEncoding>UTF-8</uriEncoding>  
  12.                 <systemProperties>  
  13.                 <java.io.tmpdir>${project.build.directory}</java.io.tmpdir>                    
  14.                 </systemProperties>                     
  15. </configuration>                    
  16.         </plugin>  
  17.           <plugin><!-- clean cargo:redeploy -->  
  18.       <groupId>org.codehaus.cargo</groupId>  
  19.       <artifactId>cargo-maven2-plugin</artifactId>  
  20.       <configuration>  
  21.         <container>  
  22.           <containerId>tomcat6x</containerId>  
  23.           <type>remote</type>  
  24.         </container>  
  25.         <configuration>  
  26.           <type>runtime</type>  
  27.         </configuration>  
  28.       </configuration>  
  29.     </plugin>  

      說明:

     1.port埠號

      2.path以/ 這樣在訪問時,就不用加入專案名

 第三步:配置執行,點選test-maven-console專案Run As-->Run Configurations   然後點選Maven Build右擊New 在Goals 輸入tomcat6:run   然後點選run執行,檢視Console有沒有報錯。

    


這時後臺沒報錯,說明能正常執行

Maven 提供的外掛很多

  1.                  <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-resources-plugin</artifactId>  
  4.     <version>2.6</version>  
  5. </plugin>  
  6. <!-- install外掛 -->  
  7. <plugin>  
  8.     <groupId>org.apache.maven.plugins</groupId>  
  9.     <artifactId>maven-install-plugin</artifactId>  
  10.     <version>2.4</version>  
  11. </plugin>  
  12. <!-- clean外掛 -->  
  13. <plugin>  
  14.     <groupId>org.apache.maven.plugins</groupId>  
  15.     <artifactId>maven-clean-plugin</artifactId>  
  16.     <version>2.5</version>  
  17. </plugin>  

比較常用到的外掛

 總結一下:

     執行Maven Web 專案比較常用的是這兩種執行,Tomcat執行Maven Web 專案還可以採用另外一種,我們平常比較少用,這裡就不具體詳細的介紹,

採用的是tomcat:redeploy命令,把Maven Web 專案釋出到外部已啟動的Tomcat進行測試