eclipse構建及運行maven web項目


1:環境

eclipse indigo,

JDK1.6,

maven 3.2.1,

tomcat7.0.42

2:安裝eclipse maven插件 m2eclipse

第一種方法:從網上下載m2eclipse,這個網上有很多下載的地方。然后放到eclipse安裝目錄的plugins下。

第二種方法:打開eclipse->help->install new software。在work with后輸入:http://download.eclipse.org/technology/m2e/releases。如圖:

3:下載maven和tomcat

上apache官網下載maven:http://maven.apache.org/download.cgi。下載完成后解壓即可。

上apache官網下載tomcat:http://tomcat.apache.org/

4:eclipse配置maven

window-》prefrences-》maven-》user setting。如圖

window-》prefrences-》maven-》installations。如圖

 

5:配置tomcat和maven

進入tomcat_home/conf/tomcat_users.xml:修改如下:

[html]  view plain  copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. <role rolename="admin-gui"/>  
  2.     <role rolename="admin-script"/>  
  3.     <role rolename="manager-gui"/>  
  4.     <role rolename="manager-script"/>  
  5.     <role rolename="manager-jmx"/>  
  6.     <role rolename="manager-status"/>  
  7.     <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>  
 


 

進入maven_home/conf/settings.xml:修改如下:

[html]  view plain  copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. <server>  
  2.         <id>tomcat</id>  
  3.         <username>admin</username>  
  4.         <password>admin</password>  
  5.     </server>  


6:eclipse創建maven web項目

這個簡單,new-》other-》maven project-》next。這里注意groupid選org.apache.maven.archetypes ,artifactid 選maven-archetype-webapp,然后next,輸入我們自己的groupid(com.test),artifactid(transition),然后finish,OK。如圖所示:

7:修改pom.xml

[html]  view plain  copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.test</groupId>  
  5.   <artifactId>transition</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1</version>  
  8.   <name>transition Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <repositories>  
  11.     <repository>  
  12.         <id>maven_remote_1</id>  
  13.         <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>  
  14.     </repository>  
  15.   </repositories>  
  16.   <dependencies>  
  17.     <dependency>  
  18.       <groupId>junit</groupId>  
  19.       <artifactId>junit</artifactId>  
  20.       <version>3.8.1</version>  
  21.       <scope>test</scope>  
  22.     </dependency>  
  23.   </dependencies>  
  24.   <build>  
  25.     <finalName>transition</finalName>  
  26.     <plugins>  
  27.         <plugin>  
  28.             <groupId>org.codehaus.mojo</groupId>  
  29.             <artifactId>tomcat-maven-plugin</artifactId>  
  30.             <configuration>  
  31.                 <warFile>target/transition.war</warFile>  
  32.                 <server>tomcat</server>  
  33.                 <url>http://localhost:8080/manager/text</url>  
  34.                 <path>/transition</path>  
  35.             </configuration>  
  36.         </plugin>  
  37.     </plugins>  
  38.   </build>  
  39. </project>  


 

8:運行maven項目

先進入tomcat目錄啟動tomcat,然后右鍵項目 run as-》run configrations:

Goals項輸入:package tomcat:redeploy

Maven Runtime選擇我們自己的maven。如圖:

 

然后點Run,控制台顯示如下:

[plain]  view plain  copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. [INFO] Processing war project  
  2. [INFO] Copying webapp resources [E:\transition\src\main\webapp]  
  3. [INFO] Webapp assembled in [3 msecs]  
  4. [INFO] Building war: E:\transition\target\transition.war  
  5. [INFO] WEB-INF\web.xml already added, skipping  
  6. [INFO]   
  7. [INFO] <<< tomcat-maven-plugin:1.1:redeploy (default-cli) @ transition <<<  
  8. [INFO]   
  9. [INFO] --- tomcat-maven-plugin:1.1:redeploy (default-cli) @ transition ---  
  10. [INFO] Deploying war to http://localhost:8080/transition    
  11. [INFO] OK - Undeployed application at context path /transition  
  12. [INFO] OK - Deployed application at context path /transition  
  13. [INFO] ------------------------------------------------------------------------  
  14. [INFO] BUILD SUCCESS  
  15. [INFO] ------------------------------------------------------------------------  
  16. [INFO] Total time: 2.412 s  
  17. [INFO] Finished at: 2014-03-13T11:43:42+08:00  
  18. [INFO] Final Memory: 6M/15M  
  19. [INFO] ------------------------------------------------------------------------  

這里省略很多控制台輸出,出現BUILD SUCCESS 則說明部署成功,瀏覽器輸入:http://localhost:8080/transition/。顯示Hello world..

 注意:這樣成功之后項目會直接打成war包,部署到tomcat下,每次build不需要重啟tomcat。這樣致命的缺點就是,不能調試。如果需要調試且在eclipse中啟動tomcat的話。會發現右鍵項目沒有run as server,有兩種方法解決:

第一種:這時需要把項目轉變成dynamic web module。右鍵項目-》properties-》project facets,然后右邊選中dynamic web module。之后就會出現run as  server了.

第二種:右鍵項目,run/debug  as  configrations  如上第8步驟的圖。不過Goals中天上【tomcat:run】。然后run就OK了,注意這里不需要提前啟動tomcat。

       

 


免責聲明!

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



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