【Maven】发布项目到Tomcat中

【Maven】发布项目到Tomcat中

初学maven,记录学习中的点滴:

将maven项目部署到tomcat实现自动化部署,参考过许多资料,也在此做个总结


开发环境

项目 版本
开发语言 Java jdk1.7
开发工具 MyEclipse10.7
中间件 Apache Tomcat 7
Maven maven 3.2.3

操作步骤

1、配置Tomcat访问权限,要打开tomcat的manager功能;在conf文件夹下的tomcat-users.xml的标签内添加如下内容

<tomcat-users>
    <role rolename="admin-gui"/>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <user username="admin" password="admin" roles="admin-gui,manager-gui"/>
    <user username="deployer" password="deployer" roles="manager-script"/>
<tomcat-users>

2、在maven的安装路径下的settings.xml添加一个server

<server>
    <id>tomcat7</id>
    <username>deployer</username>
    <password>deployer</password>
</server>

3、在项目pom.xml 的build中配置如下内容

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <!-- 注意此处的url,修改成tomcat监听的端口即可 -->
        <url>http://localhost:8080/manager/text</url>
        <!-- 此处的名字必须和setting.xml中配置的ID一致-->
        <server>tomcat7</server>
        <username>deployer</username>
        <password>deployer</password>
        <!-- 此处的名字是项目发布的工程名-->
        <path>/yourprj</path>
        <update>true</update>
    </configuration>
</plugin>

做完以上配置就可以进行下一步了:
在项目上右键Run As 选择Maven build,然后输入命令即可完成部署
这里写图片描述
看到如下输出则部署成功

Uploaded: http://localhost:8080/manager/text/deploy?path=%2Fvip&update=true (26498 KB at 77706.3 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /vip
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.426 s
[INFO] Finished at: 2017-09-15T00:05:13+08:00
[INFO] Final Memory: 14M/244M
[INFO] ------------------------------------------------------------------------

遇到的问题

[INFO] --- tomcat7-maven-plugin:2.1:deploy (default-cli) @ vip ---
[INFO] Deploying war to http://localhost:8080/vip  
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.713 s
[INFO] Finished at: 2017-09-15T00:06:54+08:00
[INFO] Final Memory: 18M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:deploy (default-cli) on project vip: Cannot invoke Tomcat manager: Connection to http://localhost refused: 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/ConnectException

原因:Tomcat服务没有启动
解决:本地启动Tomcat即可

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.111 s
[INFO] Finished at: 2017-09-15T00:10:07+08:00
[INFO] Final Memory: 18M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:deploy (default-cli) on project vip: Cannot invoke Tomcat manager: Software caused connection abort: socket write error -> [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

原因:上传大文件的时候,没有正确响应。服务器端限制的是5M
解决:修改tomcat\conf\server.xm下的Connector节点,增加maxSwallowSize=”-1”的配置;再上传大文件的时候,服务端正确响应。

<Connector maxSwallowSize="-1" connectionTimeout="20000"port="80" protocol="HTTP/1.1" redirectPort="8443"/>

猜你喜欢

转载自blog.csdn.net/long5693525/article/details/77987060