1. 程式人生 > >eclipse構建及執行maven web專案

eclipse構建及執行maven web專案

1:環境

eclipse indigo,

JDK1.6,

maven 3.2.1,

tomcat7.0.42

2:安裝eclipse maven外掛 m2eclipse

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

3:下載maven和tomcat

4:eclipse配置maven

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

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

5:配置tomcat和maven

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

<role rolename="admin-gui"/>
	<role rolename="admin-script"/>
	<role rolename="manager-gui"/>
	<role rolename="manager-script"/>
	<role rolename="manager-jmx"/>
	<role rolename="manager-status"/>
	<user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>

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

<server>
		<id>tomcat</id>
		<username>admin</username>
		<password>admin</password>
	</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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>transition</artifactId>
  <packaging>war</packaging>
  <version>0.0.1</version>
  <name>transition Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <repositories>
  	<repository>
  		<id>maven_remote_1</id>
  		<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
  	</repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>transition</finalName>
    <plugins>
    	<plugin>
    		<groupId>org.codehaus.mojo</groupId>
    		<artifactId>tomcat-maven-plugin</artifactId>
    		<configuration>
    			<warFile>target/transition.war</warFile>
    			<server>tomcat</server>
    			<url>http://localhost:8080/manager/text</url>
    			<path>/transition</path>
  			</configuration>
    	</plugin>
    </plugins>
  </build>
</project>

8:執行maven專案

先進入tomcat目錄啟動tomcat,然後右鍵專案 run as-》run configrations:

Goals項輸入:package tomcat:redeploy

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

然後點Run,控制檯顯示如下:

[INFO] Processing war project
[INFO] Copying webapp resources [E:\transition\src\main\webapp]
[INFO] Webapp assembled in [3 msecs]
[INFO] Building war: E:\transition\target\transition.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] 
[INFO] <<< tomcat-maven-plugin:1.1:redeploy (default-cli) @ transition <<<
[INFO] 
[INFO] --- tomcat-maven-plugin:1.1:redeploy (default-cli) @ transition ---
[INFO] Deploying war to http://localhost:8080/transition  
[INFO] OK - Undeployed application at context path /transition
[INFO] OK - Deployed application at context path /transition
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.412 s
[INFO] Finished at: 2014-03-13T11:43:42+08:00
[INFO] Final Memory: 6M/15M
[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。