官方提供了Maven插件用於部署基於Maven的Web應用,不一樣版本Tomcat使用的插件不一樣,不一樣版本插件的使用也有必定區別,詳細信息可參考http://tomcat.apache.org/maven-plugin.html。下面記錄的是我在Eclipse環境(同時也在命令)中使用Tomcat Maven Plugin-2.2在Tomcat7中部署Maven Web應用的配置過程:html
TOMCAT 添加角色用戶java
打開Tomcat根目錄下conf目錄中的tomcat_user.xml,在<tomcat-users>節點下添加manager角色和用戶,用於Tomcat Maven Plugin自動部署應用,xml代碼以下:apache
<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插件tomcat
<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進行部署。app
可是哦,在我剛開始的時候,使用命令的時候,出現了好幾回這個的一個錯誤:負載均衡
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的命令窗口,這樣子遠程打包方便多了。dom
關於上面那個問題,有誰解決或者是知道問題的,請私信給我,個人猜想是,Maven的環境變量和eclipse中的Maven的setting.xml的問題,我不是很清楚,可是就是由於這個猜測採起eclipse中執行成功的。eclipse
關於上面那個問題,我作一個補充,這個問題的根本是因爲兩點會出現這個ERROR信息,第一是因爲若是你已經deploy了,再次使用deploy就會報錯,第二種緣由是由於,在pom中的userName和passWord不正確,我想起來剛開始是怎麼配置的了,我在pom中根本就沒有配置用戶名和密碼,而是引用了 %MAVEN_HOME%/conf/setting.xml 中設置的server,可是爲何在這個maven中設置了,在命令行中使用會出錯,可是在eclipse中卻沒有出錯,我就不得而知了,難道是個人環境變量有問題呢,我不是太清楚,但願你們有明白這裏給我私信一下吧,下面我給出這個錯誤的所有信息,提供給你們參考。socket
[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的服務器,我如何能作到讓插件一鍵打包,還在摸索中 。。。。。。。。。。