maven+tomcat的苦逼经历

在网上找的例子,跑不起来,后来又在网上找了例子,拼起来了,现贴出来。 


本教程主要解决两个问题:(主要是部署到Tomcat 6.x 和 Tomcat 7.x也有区别,请各自座号入座.)

1.苦逼的401问题




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





2.苦逼的403错误



maven 403 No server username specified - using default  Cannot invoke Tomcat manager: Server rened HTTP response code: 403 for URL



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





INFO] Deploying war to http://localhost:8080/report


[DEBUG] No server username specified - using default



解决方案:




1.对于苦逼的401问题和403问题,这个问题是因为你的tomcat里面木有写权限,还有你的pom.xml配置的不对。



<!-- 以下是Tomcat 7.x 系列-->

C:\tomcat\apache-tomcat-7.0.34\conf\tomcat-users.xml文件打开,添加用户和密码                      <?xml version='1.0' encoding='utf-8'?>      <tomcat-users>                    <role rolename="manager"/>        <role rolename="manager-script"/>        <role rolename="admin-gui"/>        <role rolename="manager-gui"/>          <user username="admin" password="pass!@#" roles="manager,manager-gui,admin-gui,manager-script"/>                </tomcat-users>


<!-- 以下是Tomcat 6.x 系列-->
C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf\tomcat-users.xml              <?xml version='1.0' encoding='utf-8'?>  <tomcat-users>    <role rolename="manager"/>    <role rolename="manager-script"/>    <role rolename="admin"/>    <role rolename="admin-gui"/>    <role rolename="manager-gui"/>    <user username="admin" password="pass!@#" roles="admin,manager,manager-gui,admin-gui,manager-script"/>  </tomcat-users>


C:\Documents and Settings\Administrator\.m2\settings.xml 文件打开,写入用户名和密码





<servers>



<server> 

       <id>tomcat</id> 

       <username>admin</username> 

       <password>pass!@#</password> 

</server>

       

</servers>




D:\workspaces\你的项目目录\pom.xml文件打开,写入配置信息




<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>tomcat-maven-plugin</artifactId>

<version>1.1</version>

<configuration>

<server>tomcat</server><!-- 这个server标签里面的值(tomcat)要和你的setting.xml里面的id的值(tomcat)一致-->




<username>admin</username>

<password>pass!@#</password>

<!-- Tomcat 7.x -->


<url>http://localhost:8080/manager/text</url>



<!-- Tomcat 6.x

<url>http://localhost:8080/manager</url>

-->




<!-- 这个(http://localhost:8080/manager/)是你的tomcat管理地址,如果使用Tomcat 7.x后面的/text要加上,如果使用Tomcat 6.x就不要加了,不要写/html,这个就是解决403苦逼问题的-->

<path>/report</path>

</configuration>

</plugin>

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


我的tomcat 用的6.0.41,users配置
<role rolename="tomcat"/>
<role rolename="manager-script"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="guohr" password="guohr" roles="admin,manager,manager-gui,admin-gui,manager-script"/> 

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.jd</groupId>
	<artifactId>guohr-web</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>guohr-web 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>
	</dependencies>
	<build>
		<finalName>guohr-web</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<!-- http://maven.apache.org/plugins/maven-war-plugin/ -->
					<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.0-beta-1</version>
				<configuration>
					<url>http://localhost:8080/manager</url>
					<server>tomcat</server>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>



右键demoRun As 选择 Maven build,在Goals中输入package tomcat:redeploy 



你可能感兴趣的:(tomcat)