maven+eclipse+tomcat配置過程記錄

一、在eclipse中設置maven的setting文件位置,指向你下載的maven目錄中,倉庫會自動生成。


一、在eclipse中創建maven工程,注意幾項:

   選擇Artifact ID爲maven-archetype-webapp的一項

 


二、創建目錄結構:工程右鍵:new-Source Folder,創建如下幾個目錄

       1、src/main/java

       2、src/test/java

       3、src/test/resources


三、依次設置java,resources目錄的class輸出目錄,工程右鍵:build path-Source,雙擊Source Folder,在彈出框中選擇輸出的目錄。

同時要選上Allow output folders for source folders.



四、 把項目轉成Dynamic Web項目

       右鍵項目,選擇Project Facets,點擊Convert to faceted from

  配置Project Facets

選擇java且版本1.6,勾選Dynamic Web Module的Version爲2.5。(3.0爲Java7的)。

如果提示錯誤,可能需要在Java Compiler設置Compiler compliance level 爲1.6。或者需要在此窗口的Java的Version改成1.6。

         另:如果報錯 cannot ….. Dynamic Web Module to 2.5

       打開項目根目錄下的.setting文件夾下的org.eclipse.wst.common.project.facet.core.xml文件手動修改版本

    <faceted-project>
  <fixed facet="wst.jsdt.web"/>
  <installed facet="java" version="1.6"/><編譯器1.6版,運行的JDK版本不能低於編譯的版本>
  <installed facet="jst.web" version="2.5"/><servlet2.5版>
  <installed facet="wst.jsdt.web" version="1.0"/>
    </faceted-project>

------------------------------------------------以下4行可忽略

配置 Modify Faceted Project

點擊Further configuration available…,彈出Modify Faceted Project窗口

此處是設置web.xml文件的路徑,我們輸入src/main/webapp。

Generate web.xml deployment descriptor自動生成web.xml文件,可選可不選。

--------------------------------------------------------------

五、設置項目部署時文件發佈的目錄位置

      在右鍵項目打開此窗口。在左側列表中會出現一個Deployment Assembly,點擊進去後,如下圖:

image

    1,需要刪除test的兩項,因爲test是測試使用,並不需要部署。

    2,設置將Maven的jar包發佈到lib下。

         Add -> Java Build Path Entries -> Maven Dependencies -> Finish

    設置完成後的效果圖

image


六、向maven項目添加jar包

    6.1 在pom.xml中添加所需要的jar包

    使用Maven POM editor打開項目中的pom.xml文件,選擇Dependencies,在Dependencies欄目點擊Add進行,首先彈出一個搜索按鈕,例如輸入spring-web,就會自動搜索關於spring-web相關的jar包,我們選擇3.0.5版本的spring。將spring包全部添加進來。需要添加的其他jar包有:junit、jstl。或者點擊pom.xml直接編輯pom.xml文件。這樣可以直接copy過來dependencies內容。

image

6.2設置jar包的scope

image

當添加進入一個jar包後,有一些屬性需要設置,最重要的就是scope,它有以下幾種取值:

1. compile,缺省值,適用於所有階段,會隨着項目一起發佈。

2. provided,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。

3. runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。

4. test,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發佈。

5. system,類似provided,需要顯式提供包含依賴的jar,Maven不會在 Repository中查找它。

通常SpringMVC項目所需要配置scope的jar包如下圖:

image

有的時候發現servlet-api還是被打包到lib下面了,此時肯定會報錯。就需要把maven插件中的WTP也安裝一下。

Eclipse在線安裝路徑:http://m2eclipse.sonatype.org/sites/m2e-extras。選擇for Eclipse WTP。

-------------------------------------------------------------------------------------------------以下搭建spring框架部分可忽略

七、構建SpringMVC框架

7.1 編輯web.xml文件

需要添加log4j,字符過濾,Spring 的dispatcher等。

webx.xml代碼如下:

Xml代碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         version="2.5" >
    
    <context-param>
        <param-name></param-name>
        <param-value></param-value>
    </context-param>

    <!-- Spring的log4j監聽器 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- 字符集 過濾器  -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring view分發器 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

7.2 編寫Spring配置文件dispatcher-servlet.xml

如要添加MVC驅動、註解檢測、視圖解析等。dispatcher-servlet.xml代碼如下:

Xml代碼

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.xujj" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

-------------------------------------------------------------------------------------------------以上爲可忽略部分

八、maven整合tomcat,支持熱部署

1、在tomcat中的conf的tomcat_users.xml中創建管理用戶和密碼,一般選manager-script,在pom.xml中的tomcat插件中用text,否則可能出現403錯誤

<tomcat-users> 
<role rolename="manager-script"/>
<user username="admin" password="password" roles="manager-script"/>
</tomcat-users>

tomcat role changed:
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access.
manager-gui - allows access to the HTML GUI and the status pages
manager-script - allows access to the text interface and the status pages
manager-jmx - allows access to the JMX proxy and the status pages
manager-status - allows access to the status pages only
只用manager-gui role即可。

2、啓動tomcat,然後訪問 http://localhost:8080/manager/html,輸入admin/password,如果tomcat默認界面,表示tomcat一切OK,然後纔可以繼續後面操作。

3、在項目根據目錄中的pom.xml文件中的build中配置tomcat插件

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://localhost:8080/manager/text</url>

<!--

2、deploy:eg http://localhost:8080/manager/deploy?path=/phoenix&war=&update=true
but the deploy url does not find.
the warning is as follows:
The page you tried to access (/manager/deploy) does not exist.
The Manager application has been re-structured for Tomcat 7 onwards and some of URLs have changed. All URLs used to access the Manager application should now start with one of the following options:
/manager/html for the HTML GUI
/manager/text for the text interface
/manager/jmxproxy for the JMX proxy
/manager/status for the status pages

-->
<username>admin</username>
<password>admin</password>
<path>/baseinfo</path>
</configuration>
</plugin>

</plugins>

1、執行tomcat:deploy時出現以下錯誤,是由於tomcat-users中配置的用戶名或者密碼與pom.xml中的不一致

Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project webs: Cannot invoke Tomcat manager: Server returned HTTP response code: 401 for URL: http://localhost:8080/manager/text/deploy?path=%2Fwebs&war= -> [Help 1]

重啓tomcat,再次執行命令

2、必須啓動tomcat後再執行tomcat:deploy或者tomcat:redeploy,否則報連接被拒絕的錯誤信息

Cannot invoke Tomcat manager: Connection refused: connect

3、403錯誤是因爲tomcat的角色設置問題,修改角色名即可

再打開tomcat的webapps目錄,已經將項目部署到該文件夾中,瀏覽器打開頁面測試成功。


記錄配置中涉及到的相關插件和知識:

1、有提到使用maven-war-plugin插件來生成war,但不使用該插件在tomcat中同樣生成了項目的war包,

     <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration> 
<packagingExcludes>WEB-INF/web.xml</packagingExcludes> 
</configuration>
</plugin>

2、第一個項目在pom.xml中配置了以下節點,不知是否可省略

<repositories>
<repository>
<id>people.apache.snapshots</id>
<url>http://repository.apache.org/content/groups/snapshots-group/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>  
   <id>apache.snapshots</id>  
   <name>Apache Snapshots</name>  
   <url>  
       http://repository.apache.org/content/groups/snapshots-group/  
   </url>  
   <releases>  
       <enabled>false</enabled>  
   </releases>  
   <snapshots>  
       <enabled>true</enabled>  
   </snapshots>  
</pluginRepository>
</pluginRepositories>


發佈了11 篇原創文章 · 獲贊 6 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章