Jetty和Tomcat執行Maven Web專案幾種方法
目錄
1.採用Jetty執行Maven Web專案
2.採用Tomcat執行Maven Web專案
1、採用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>
第三步:配置執行埠等。點選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進行測試