Eclipse集成Maven的Web工程demo(獨立及Maven集成tomcat)


用到的工具
JDK1.8
Eclipse Luna j2ee
Eclipse 集成的Mave
tomcat7 (集成在xampp中的獨立web服務器,或者通過Maven plugin集成web服務器)

步驟如下
1.新建Mave web工程,

A

B

 

 

 

C

D

 之后會得到如下工程樹

可以看到有紅色警告,這是因為缺少java web服務器,可以安裝tomcat,然后在eclipse中添加server並部署web程序。

也可以通過Maven plugin集成tomcat,使用maven發布web程序,可以實現自動部署web程序。

2.配置tomcat

安裝xampp之后,就集成了tomcat7,但是此時不要單獨啟動tomcat,稍后會在eclipse中啟動。

配置步驟如下,在工程屬性的runtime中new一個server,類型為tomcat7

 

填寫tomcat安裝目錄,就在xampp中找,

要確保工程已經被添加進server中,如果沒有,需要手動添加進去(在server上右鍵,add and remove)

添加完了server,工程目錄已經沒有警告了,此時可以運行這個web項目了

 

 在index.jsp上右鍵選擇run->run on server就行了

 

3. 使用Maven的tomcat插件部署web程序。

前面使用的是獨立的tmcat部署的方式,既然我們創建的是maven工程,通過maven來集成各種第三方工具(例如web服務器)才能充分發揮maven的優勢。

我們在pom文件中添加如下兩段,就可以實現集成tomcat服務器,並自動發布的功能。

首先是jsp依賴庫,

 1     <dependencies>
 2         <dependency>
 3             <groupId>junit</groupId>
 4             <artifactId>junit</artifactId>
 5             <version>3.8.1</version>
 6             <scope>test</scope>
 7         </dependency>
 8         <dependency>
 9             <groupId>javax.servlet</groupId>
10             <artifactId>servlet-api</artifactId>
11             <version>2.5</version>
12             <scope>provided</scope>
13         </dependency>
14         <dependency>
15             <groupId>javax.servlet.jsp</groupId>
16             <artifactId>jsp-api</artifactId>
17             <version>2.2</version>
18             <scope>provided</scope>
19         </dependency>
20         <dependency>
21             <groupId>javax.servlet</groupId>
22             <artifactId>jstl</artifactId>
23             <version>1.2</version>
24         </dependency>
25         <dependency>
26             <groupId>jsptags</groupId>
27             <artifactId>pager-taglib</artifactId>
28             <version>2.0</version>
29             <scope>provided</scope>
30         </dependency>
31     </dependencies>

然后是配置maven的tomcat插件,放在<build>里面即可,

 1   <build> 
 2     <finalName>maven-web-demo</finalName>  
 3     <plugins> 
 4       <plugin> 
 5         <groupId>org.apache.tomcat.maven</groupId>  
 6         <artifactId>tomcat7-maven-plugin</artifactId>  
 7         <version>2.2</version>  
 8         <configuration> 
 9           <port>8080</port>  
10           <path>/maven-web-demo</path>  
11           <uriEncoding>UTF-8</uriEncoding>  
12           <finalName>maven-web-demo</finalName>  
13           <server>tomcat7</server> 
14         </configuration>  
15         <executions> 
16           <!-- 打包成功后即開始運行web容器 -->  
17           <execution> 
18             <phase>package</phase>  
19             <goals> 
20               <goal>run</goal> 
21             </goals> 
22           </execution> 
23         </executions> 
24       </plugin> 
25     </plugins> 
26   </build>

再配置一下maven run

在run configuation中,配置Maven run

 

 

 上面的關鍵點是在Goals中填入 tomcat:run,之后直接點下面的 Run按鈕,可以看到控制台結果,

看到8080說明已經發布成功了,在瀏覽器輸入 http://localhost:8080/maven-web-demo/

要注意的是,maven中最好指定一下java版本號,我最開始用的java8,但是maven中沒有指定版本(估計默認是java7),

始終編譯不過,百思不得其解,最后換成java7了才能跑,然后恍然大悟。eclipse,maven,本機java之間需要保持統一才行。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM