Maven自动部署war包到tomcat

步骤一: 

在tomcat安装目录下找到tomcat-users.xml文件。该文件路径为【tomcat安装根目录】 /conf/ 

修改文件内容,增加下列内容

 

  1. <role rolename="manager"/>
  2. <user username="tomcat" password="123456" roles="manager"/>


步骤二: 

在你的项目的pom.xml文件中,增加下列<plugin></plugin>中内容: 

 

  1. <build>
  2. 。。。。。
  3. <plugins>
  4. 。。。。。
  5. <plugin>
  6. <groupId>org.codehaus.mojo</groupId>
  7. <artifactId>tomcat-maven-plugin</artifactId>
  8. <configuration>
  9. <url>http://localhost:8080/manager</url>
  10. <username>tomcat</username>
  11. <password>123456</password>
  12. <path>/${finalName}</path>
  13. </configuration>
  14. </plugin>
  15. 。。。
  16. </plugins>
  17. </build>


其中username和password就是tomcat中配置的username和password。 
当然在pom.xml的<properties>标签中,还需要定义war包的名字。假如只写成<path>/</path>,则部署的war包名字为ROOT.war。 
在pom.xml的<properties>标签中,定义war包名字内容如下: 

 

  1. <properties>
  2. <finalName>SSHMJ-FRANK</finalName>
  3. </properties>


 然后运行maven命令:
mvn:tomcat:redeploy
或是直接在eclipse里面点 “run as”运行maven命令,如下图:


命令运行完成后,在tomcat的webapps目录下可见已经自动部署过去的war包,如下图:

 

开始运行自动部署命令时,一定要先启动tomcat。否则会报下列错误:

 

  1. [INFO]
  2. [INFO] --- tomcat-maven-plugin:1.0:redeploy (default-cli) @ SSHMJ-FRANK ---
  3. [INFO] Deploying war to http://localhost:8080/SSHMJ-FRANK  
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] BUILD FAILURE
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] Total time: 9.630s
  8. [INFO] Finished at: Tue Aug 31 16:35:52 CST 2010
  9. [INFO] Final Memory: 6M/15M
  10. [INFO] ------------------------------------------------------------------------
  11. [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]
  12. [ERROR]
  13. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  14. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  15. [ERROR]
  16. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  17. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

 

最后得到的完整pom配置如下:

 

<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.shansun.http.rmi</groupId>
  <artifactId>HttpRMI</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>HttpRMI Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
		<finalName>HttpRMI</finalName>
	</properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>HttpRMI</finalName>
    <plugins>
			<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>tomcat-maven-plugin</artifactId>
			<configuration>
				<url>http://127.0.0.1:8888/manager</url>
				<username>tomcat</username>
				<password>tomcat</password>
				<path>/${finalName}</path>
			</configuration>
			</plugin>
		</plugins>
  </build>
</project>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值