Jetty和Tomcat運行Maven Web項目幾種方法

來源:互聯網
上載者:User

標籤:maven   nexus   java   jar   

  目錄

   1.簡介

   2.採用Jetty和Tomcat運行Maven Web項目

  

 1.簡介

    上一篇我們介紹了用Eclipse建立了Maven Web項目,接下來我們介紹怎麼運行Maven Web項目方便我們開發和調試,這裡使用的是Maven外掛程式的形式引用進來的,這樣耦合性比較低,我們需要什麼樣的工具就引入對應的外掛程式就可以用。


 2.採用Jetty和Tomcat運行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中添加

         

<plugins>  <plugin>        <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <configuration>            <source>${version.jdk}</source>                <target>${version.jdk}</target>                <showWarnings>true</showWarnings><compilerArguments><verbose /><bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath></compilerArguments>                            </configuration>     </plugin>           <plugin><!-- clean  -Djetty.port=9090 jetty:run -->          <groupId>org.mortbay.jetty</groupId>          <artifactId>jetty-maven-plugin</artifactId>          <configuration>            <reload>automatic</reload>            <scanIntervalSeconds>10</scanIntervalSeconds>            <systemProperties>            </systemProperties>            <useTestClasspath>true</useTestClasspath>            <webAppConfig>              <contextPath>/</contextPath>            </webAppConfig>          </configuration>        </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-plugin和tomcat6-maven-plugin了,而groupId也由org.codehaus.mojo改為org.apache.tomcat.maven。)我們這邊使用的是tomcat6-maven-plugin 

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

      

    <!-- tomcat運行 clean tomcat6:run -->        <plugin>        <groupId>org.apache.tomcat.maven</groupId>          <artifactId>tomcat6-maven-plugin</artifactId>          <version>2.2</version>          <configuration><!-- http port -->      <port>9090</port>      <!-- application path always starts with /-->      <path>/</path>      <uriEncoding>UTF-8</uriEncoding>      <systemProperties>              <java.io.tmpdir>${project.build.directory}</java.io.tmpdir>                 </systemProperties>      </configuration>           </plugin>              <plugin><!-- clean cargo:redeploy -->          <groupId>org.codehaus.cargo</groupId>          <artifactId>cargo-maven2-plugin</artifactId>          <configuration>            <container>              <containerId>tomcat6x</containerId>              <type>remote</type>            </container>            <configuration>              <type>runtime</type>            </configuration>          </configuration>        </plugin>

      說明:

     1.port連接埠號碼

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

 第三步:配置運行,點擊test-maven-console項目Run As-->Run Configurations   然後點擊Maven Build右擊New 在Goals 輸入tomcat6:run   然後點擊run運行,查看Console有沒有報錯。

    




這時後台沒報錯,說明能正常運行

  

Maven 提供的外掛程式很多

   

                    <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.6</version></plugin><!-- install外掛程式 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-install-plugin</artifactId><version>2.4</version></plugin><!-- clean外掛程式 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-clean-plugin</artifactId><version>2.5</version></plugin>

   

比較常用到的外掛程式


 總結一下:

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

採用的是tomcat:redeploy命令,把Maven Web 項目發布到外部已啟動的Tomcat進行測試

  在使用Maven 引入JAR 包時,有時會報找不到,這裡我在maven使用過程中遇到的問題(依賴jar檔案下載失敗等)都有介紹,希望能協助。


      

      

    

Jetty和Tomcat運行Maven Web項目幾種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.