官方提供了Maven插件用于部署基于Maven的Web应用,不同版本Tomcat使用的插件不同,不同版本插件的使用也有一定区别,详细信息可参考http://tomcat.apache.org/maven-plugin.html。下面记录的是我在Eclipse环境(同时也在命令)中使用Tomcat Maven Plugin-2.2在Tomcat7中部署Maven Web应用的配置过程:
TOMCAT 添加角色用户
打开Tomcat根目录下conf目录中的tomcat_user.xml,在<tomcat-users>节点下添加manager角色和用户,用于Tomcat Maven Plugin自动部署应用,xml代码如下:
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="123456" roles="manager-gui, manager-script"/> <!-- 用户名和密码可以是其他字合法符串 -->
</tomcat-users>
配置Tomcat Maven Plugin插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url> <!-- 如果使用的是tomcat7,一定要保持这个URL -->
<!-- <server>deploymentRepo</server> 在maven中的设置,如果配了下面的用户名和密码不需要存在了-->
<username>tomcat</username> <!-- 在tomcat.user中的配置的密码和用户保持一致 -->
<password>123456</password>
<path>/${project.build.finalName}</path>
<!-- war文件路径缺省情况下指向target -->
<!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile>-->
</configuration>
</plugin>
部署应用
在项目目录下,进入CMD命令窗口,使用 mvn clean 首先进行清理工作,如果是第一次部署的话,使用命令 mvn tomcat7:deploy -Dmaven.test.skip=true -e进行部署(后面的参数代表跳过测试环节 -e将全部的日志信息打印出来),但是如果第二次部署的话,使用命令mvn tomcat7:redeploy -Dmaven.test.skip=true -e进行部署。
但是哦,在我刚开始的时候,使用命令的时候,出现了好几次这个的一个错误:
Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project jobwisdom: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]
我也不知道那个是怎么解决的,但是我最后使用了eclipse的命令,执行了一次maven update后,在eclipse和命令中打包都正确了,下面我给出eclipse的命令窗口,这样子远程打包方便多了。
关于上面那个问题,有谁解决或者是知道问题的,请私信给我,我的猜测是,Maven的环境变量和eclipse中的Maven的setting.xml的问题,我不是很清楚,但是就是因为这个猜想采取eclipse中执行成功的。
关于上面那个问题,我做一个补充,这个问题的根本是由于两点会出现这个ERROR信息,第一是由于如果你已经deploy了,再次使用deploy就会报错,第二种原因是因为,在pom中的userName和passWord不正确,我想起来刚开始是怎么配置的了,我在pom中根本就没有配置用户名和密码,而是引用了 %MAVEN_HOME%/conf/setting.xml 中设置的server,但是为什么在这个maven中设置了,在命令行中使用会出错,但是在eclipse中却没有出错,我就不得而知了,难道是我的环境变量有问题呢,我不是太清楚,希望大家有明白这里给我私信一下吧,下面我给出这个错误的全部信息,提供给大家参考。
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project world-project-cms: Cannot invoke Tomcat manager: Connection reset by peer: socket write error -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project world-project-cms: Cannot invoke Tomcat manager
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Cannot invoke Tomcat manager
at org.apache.tomcat.maven.plugin.tomcat7.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:141)
at org.apache.tomcat.maven.plugin.tomcat7.AbstractWarCatalinaMojo.execute(AbstractWarCatalinaMojo.java:68)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 20 more
Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:181)
at org.apache.http.impl.conn.LoggingSessionOutputBuffer.write(LoggingSessionOutputBuffer.java:73)
at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:115)
at org.apache.tomcat.maven.common.deployer.TomcatManager$RequestEntityImplementation.writeTo(TomcatManager.java:880)
at org.apache.http.entity.HttpEntityWrapper.writeTo(HttpEntityWrapper.java:89)
at org.apache.http.impl.client.EntityEnclosingRequestWrapper$EntityWrapper.writeTo(EntityEnclosingRequestWrapper.java:108)
at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:117)
at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:265)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.sendRequestEntity(ManagedClientConnectionImpl.java:203)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:236)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:682)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.tomcat.maven.common.deployer.TomcatManager.invoke(TomcatManager.java:742)
at org.apache.tomcat.maven.common.deployer.TomcatManager.deployImpl(TomcatManager.java:705)
at org.apache.tomcat.maven.common.deployer.TomcatManager.deploy(TomcatManager.java:388)
at org.apache.tomcat.maven.plugin.tomcat7.deploy.AbstractDeployWarMojo.deployWar(AbstractDeployWarMojo.java:85)
at org.apache.tomcat.maven.plugin.tomcat7.deploy.AbstractDeployMojo.invokeManager(AbstractDeployMojo.java:82)
at org.apache.tomcat.maven.plugin.tomcat7.AbstractCatalinaMojo.execute(AbstractCatalinaMojo.java:132)
... 23 more
ON!我知道了,原来我在Maven的配置setting中,设置了一个server,定义了服务器的用户名和密码,然后,我又在pom中写了server,并且还写了一个userName的标签,结果他最后读取的是pom中的用户名,忽略了Maven 的设置,结果又TMD在pom没有找到密码,就出现了这种错误,日。
还有一个问题,就是当我有负载均衡的时候,N台IP的服务器,我如何能做到让插件一键打包,还在摸索中 。。。。。。。。。。
来源:oschina
链接:https://my.oschina.net/u/2392159/blog/757537