一步一步學習SSH(1)——Maven&Tomcat自動部署

正所謂,工欲善其事,必先利其器,今天開始正式系統的學習Struts2+Hibernate+Spring4,這裏記錄一下學習的過程。

Maven配置Tomcat自動部署,網上看了很多中文資料,感覺亂七八糟的,最後配置還出錯了,後來乾脆在官網上直接找了。

官網:http://tomcat.apache.org/maven-plugin-2.2/ 

部署命令: http://tomcat.apache.org/maven-plugin-2.2/context-goals.html 


我用的是Intellij idea,eclipse下面的配置應該也是大同小異的。pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <name>myBlog</name>
    <parent>
        <groupId>${project.parent.groupId}</groupId>
        <artifactId>${project.parent.artifactId}</artifactId>
        <version>${project.parent.version}</version>
        <relativePath>../parent/</relativePath>
    </parent>
    <groupId>com.zy.demo.blog</groupId>
    <artifactId>blog</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <webXml>webapp\WEB-INF\web.xml</webXml>
                    <warSourceDirectory>webapp</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>

        <strong><pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat6-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <server>myserver</server>
<span style="white-space:pre">			<path>/blogTest</path></span>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement></strong>
    </build>


</project>


上面其他的可以忽略,主要是pluginManagement 標籤內的內容。配置了兩個tomcat,如果只有一個的話,配置一個就夠了。groupId 和artifactId 如上,目前官方最新版爲2.2。<path>指的是發佈在tomcat項目war文件的名稱。如:blogTest.war。

<strong> <configuration>
               <server>myserver</server>
 </configuration></strong>
server中的myserver 和maven的配置文件settings.xml配置相對應,即,要在settings.xml加入:

    <server>
      <id>myserver</id>
      <username>admin</username>
      <password>admin</password>
    </server>

上面的username 和password就是tomcat的username和password,找到tomcat的配置文件conf/tomcat-users.xml,
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="admin" roles="manager-gui,manager-script"/>
注意不僅僅需要manager-gui,還需要manager-script,不然會報錯。

根據官網,還需要在settings.xml文件中加入:

<pluginGroups>
    ....
    <pluginGroup>org.apache.tomcat.maven</pluginGroup>
    ....
  </pluginGroups>

至此,啓動tomcat(必須先啓動tomcat),然後再在控制檯運行:

mvn clean install
mvn tomcat7:redeploy
或者在intellij idea 中運行:


至此,完成。此後得出一個結論,以後無論幹嘛,儘量少看二手資料,而直接去官網上看。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章