zoukankan      html  css  js  c++  java
  • 使用Maven自动部署Java Web项目到Tomcat问题小记

    导读

    首先说说自己为啥要用maven管理项目,一个直接的原因是:我在自己电脑上开发web项目,每次部署到服务器上时都要经历如下步骤:

    1. 首先在Eclipse里将项目打包成war包
    2. 将服务器上原来的项目文件夹删掉
      cd /var/lib/tomcat7/webapps
      sudo rm XXX.war
      sudo rm -rf XXX
    3. 将war包传到服务器上,比如用pscp命令上传
      pscp -pw "xxx" XXX.war username@ip:/var/lib/tomcat7/webapps
    4. 重启tomcat
      sudo service tomcat7 restart

    每次都这些步骤,非常烦人,而用maven来管理就不需要这些步骤啦,直接在Eclipse里配置maven插件,然后使用maven来自动部署项目,关于怎么自动部署可网上很多教程,具体可参看后面的参考资料,部署成功后只需要用一个命令即可自动将我的web项目部署到tomcat服务器上,我一般用下面这样的命令:

    mvn tomcat7:deploy -Dmaven.test.skip=true

    其中-Dmaven.test.skip=true表示临时性跳过测试代码的编译(也可用-DskipTests表示跳过测试阶段),maven.test.skip同时控制maven-compiler-plugin和maven-surefire-plugin两个插件的行为,即跳过编译,又跳过测试。

    但初次按着教程来总是遇到各种问题, 下面记录我在部署过程中遇到的各种问题及注意事项,以提供参考意义。

    maven使用问题小记

    1. maven配置文件pom.xml里的tomcat插件一般像下面这样配置:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.1</version>
        <configuration>
           <path>/test</path>
           <url>http://localhost:8080/manager/</url>
           <server>tomcat</server>
        </configuration>
      </plugin>

      里面的server需要在maven的配置文件settings.xml里配置如下:

      <server>
        <id>tomcat</id>
        <username>admin</username>
        <password>123456</password>
      </server>

      这里的username和password一般为tomcat server的用户名和密码。

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

      [INFO]   
      [INFO] --- tomcat-maven-plugin:1.0:redeploy (default-cli) @ SSHMJ-FRANK ---  
      [INFO] Deploying war to http://localhost:8080/SSHMJ-FRANK    
      [INFO] ------------------------------------------------------------------------  
      [INFO] BUILD FAILURE  
      [INFO] ------------------------------------------------------------------------  
      [INFO] Total time: 9.630s  
      [INFO] Finished at: Tue Aug 31 16:35:52 CST 2010  
      [INFO] Final Memory: 6M/15M  
      [INFO] ------------------------------------------------------------------------  
      [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]  
      [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
    3. HTTP 403错误

      [ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat-maven-plugin: 1.1:
      deploy (default-cli) on project XXX: Cannot invoke Tomcat manager: 
      Server returned HTTP response code: 403 for URL: 
      http://localhost:8080/manager/html/deploy?path=XXX -> [Help 1]

      网上有人说产生该问题有可能因为两个原因:

      1)如果使用的是Tomcat 7,需要修改pom.xml中部署的url地址,将

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

      改为

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

      2)tomcat用户权限分配问题,需要同时具备manager-gui和manager-script权限,比如忘了分配manager-script权限。

      正确的conf/tomcat-users.xml配置应为:

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

      不过我的问题都不是上面两个,我的问题是自动部署命令写错了,应该是mvn tomcat7:deploy命令,而我之前用的是mvn tomcat:deploy命令

    4. “Application already exists at path”问题 使用tomcat7-maven-plugin插件部署到tomcat服务器时,当服务器上已经有相同名字的项目就会导致

      FAIL - Application already exists at path ...

      解决方法是在pom.xml文件中配置tomcat7-maven-plugin插件时加入参数update

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0-SNAPSHOT</version>
        <configuration>
      	  <url>http://XXX:8080/manager/html</url>
      	  <server>tomcat</server>
      	  <username>admin</username>
      	  <password>12345</password>
      	  <path>/${finalName}</path>
      	  <update>true</update>
         </configuration>
      </plugin>
    5. “web.xml which will be ignored ”问题 在使用Maven 编译项目的时候会出现:

      [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
      (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')

      解决方法是添加下面这样一个plugin即可:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
           <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
        </configuration>
      </plugin>

    参考资料

    去tomcat官网http://tomcat.apache.org/,左侧栏Apache Tomcat下的Maven Plugin,点进去选择最新版本Version 2.2

    通过介绍可知,使用tomcat的maven插件有两种配置方式:

    第一种:在pom.xml文件的<build></build>中加入如下配置:

    1. <pluginManagement>  
    2.     <plugins>  
    3.         <plugin>  
    4.             <groupId>org.apache.tomcat.maven</groupId>  
    5.             <artifactId>tomcat6-maven-plugin</artifactId>  
    6.             <version>2.2</version>  
    7.         </plugin>  
    8.         <plugin>  
    9.             <groupId>org.apache.tomcat.maven</groupId>  
    10.             <artifactId>tomcat7-maven-plugin</artifactId>  
    11.             <version>2.2</version>  
    12.         </plugin>  
    13.     </plugins>  
    14. </pluginManagement>  

    这种配置是针对某一个项目的,只对一个项目生效。

    第二种:在maven的setting.xml文件中加入如下配置:

    1. <pluginGroups>  
    2.     <pluginGroup>org.apache.tomcat.maven</pluginGroup>  
    3. </pluginGroups>  

    这种在maven插件上的配置会对所有的项目起作用。

    配置好之后,就可以启动项目看效果了。

    使用Maven Build启动项目,Goals那一栏填:

    tomcat6:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080

    或者填:

    tomcat7:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080

    其中,

    -Dmaven.tomcat.uriEncoding=UTF-8 这个配置最好始终加上

    -Dmaven.tomcat.path=/ 这个配置可以不加,默认使用/${artifactId},此处的artifactId即建pom.xml文件时写的那个artifactId,一般为项目名。如果配置为/的话,届时访问的路径就是hostname:port/,如果配置为/test的话,则访问路径是hostname:port/test,相当于namesapce的作用。

    -Dmaven.tomcat.port=8080 这个配置可以设置,默认是8080

    以上两种启动方式的区别仅在于使用的tomcat的版本不一样。如果使用tomcat7的话,则如果配置方式是在pom.xml文件中配置的话,则必须配置tomcat7-maven-plugin,否则会BUILD FAILURE;如果配置方式是配置maven的setting.xml文件的话,则无所谓,<pluginGroup>org.apache.tomcat.maven</pluginGroup>这一行的作用是把所有版本的maven的tomcat插件及相关插件都下载下来了。使用tomcat6的话也同理。

  • 相关阅读:
    SpringJDBC源码分析记录
    RHEL7使用NAT方式上网
    SQL优化参考
    IDEA引入Gradle工程小记
    OAuth2.0原理与实现
    sudo用法记录
    ZooKeeper单机伪集群搭建与启动
    Netty实践与NIO原理
    Spring Security原理与应用
    Winform 生成不需要安装的exe可执行文件 ILMerge使用
  • 原文地址:https://www.cnblogs.com/goody9807/p/6567910.html
Copyright © 2011-2022 走看看