转
http://darkranger.iteye.com/blog/751892
步骤一:
在tomcat安装目录下找到tomcat-users.xml文件。该文件路径为【tomcat安装根目录】 /conf/
修改文件内容,增加下列内容:
- <role rolename="manager"/>
- <user username="tomcat" password="123456" roles="manager"/>
<role rolename="manager"/>
<user username="tomcat" password="123456" roles="manager"/>
步骤二:
在你的项目的pom.xml文件中,增加下列<plugin></plugin>中内容:
- <build>
- 。。。。。
- <plugins>
- 。。。。。
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>tomcat-maven-plugin</artifactId>
- <configuration>
- <url>http://localhost:8080/manager</url>
- <username>tomcat</username>
- <password>123456</password>
- <path>/${finalName}</path>
- </configuration>
- </plugin>
- 。。。
- </plugins>
- </build>
<build>
。。。。。
<plugins>
。。。。。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager</url>
<username>tomcat</username>
<password>123456</password>
<path>/${finalName}</path>
</configuration>
</plugin>
。。。
</plugins>
</build>
其中username和password就是tomcat中配置的username和password。
当然在pom.xml的<properties>标签中,还需要定义war包的名字。假如只写成<path>/</path>,则部署的war包名字为ROOT.war。
在pom.xml的<properties>标签中,定义war包名字内容如下:
- <properties>
- <finalName>SSHMJ-FRANK</finalName>
- </properties>
<properties>
<finalName>SSHMJ-FRANK</finalName>
</properties>
然后运行maven命令:
mvn:tomcat:redeploy
或是直接在eclipse里面点 “run as”运行maven命令,如下图:
命令运行完成后,在tomcat的webapps目录下可见已经自动部署过去的war包,如下图:
提示:
开始运行自动部署命令时,一定要先启动tomcat。否则会报下列错误:
[INFO]
[INFO] --- tomcat-maven-plugin:1.0:redeploy (default-cli) @ SSHMJ-FRANK ---
[INFO] Deploying war to http://localhost:8080/SSHMJ-FRANK
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.630s
[INFO] Finished at: Tue Aug 31 16:35:52 CST 2010
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.0:redeploy (default-cli) on project SSHMJ-FRANK: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
完整的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.eastcom.maven</groupId>
<artifactId>MV_TEST</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MV_TEST Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<finalName>MVTEST</finalName>
</properties>
<build>
<finalName>MV_TEST</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager</url>
<username>tomcat</username>
<password>123456</password>
<path>/${finalName}</path>
</configuration>
</plugin>
</plugins>
</build>
</project>