将maven项目打war包并部署到Tomcat上

1、环境:eclipse、apache-maven-3.3.3、apache-tomcat-7.0.63

2、配置:

(1)配置Tomcat的manager访问权限:在F:\software\apache-tomcat-7.0.63\conf\tomcat-users.xml下做如下修改

[html] view plain copy
  1. <tomcat-users>  
  2. <!--  
  3.   NOTE:  By default, no user is included in the "manager-gui" role required  
  4.   to operate the "/manager/html" web application.  If you wish to use this app,  
  5.   you must define such a user - the username and password are arbitrary.  
  6. -->  
  7. <!--  
  8.   NOTE:  The sample user and role entries below are wrapped in a comment  
  9.   and thus are ignored when reading this file. Do not forget to remove  
  10.   <!.. ..> that surrounds them.  
  11. -->  
  12. <!--  
  13.   <role rolename="tomcat"/>  
  14.   <role rolename="role1"/>  
  15.   <user username="tomcat" password="tomcat" roles="tomcat"/>  
  16.   <user username="both" password="tomcat" roles="tomcat,role1"/>  
  17.   <user username="role1" password="tomcat" roles="role1"/>  
  18. -->  
  19.     <role rolename="admin-gui"/>  
  20.     <role rolename="admin-script"/>  
  21.     <role rolename="manager-gui"/>  
  22.     <role rolename="manager-script"/>  
  23.     <role rolename="manager-jmx"/>  
  24.     <role rolename="manager-status"/>  
  25.     <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>  
  26. </tomcat-users>  
(2)apache-maven-3.3.3配置:为了让maven可以访问tomcat的权限,所以需要把如上创建的用户添加到settings.xml中

[html] view plain copy
  1. <servers>  
  2.   
  3.     <!-- 配置tomcat-/manager/text 访问权限 -->  
  4.     <server>  
  5.       <id>tomcat</id>  
  6.       <username>admin</username>  
  7.       <password>admin</password>  
  8.     </server>  
  9.   
  10.   </servers>  

(3)配置工程目录下的pom.xml文件,加入build,并配置tomcat7的maven插件,如下配置

[html] view plain copy
  1. <build>  
  2.         <!--  <finalName>hxyc</finalName> -->  
  3.     <!-- directory缺省情况下指向target -->   
  4.                <!--<directory>${basedir}/target</directory>-->  
  5.     <plugins>  
  6.         <plugin>  
  7.             <groupId>org.apache.maven.plugins</groupId>  
  8.             <artifactId>maven-compiler-plugin</artifactId>  
  9.             <version>2.3.2</version>  
  10.             <configuration>  
  11.                 <source>1.7</source>  
  12.                 <target>1.7</target>  
  13.             </configuration>  
  14.         </plugin>  
  15.         <plugin>  
  16.             <groupId>org.apache.tomcat.maven</groupId>  
  17.             <artifactId>tomcat7-maven-plugin</artifactId>  
  18.             <version>2.2</version>  
  19.             <configuration>  
  20.                 <port>8080</port>  
  21.                 <url>http://localhost:8080/hxyc</url>  
  22.                 <server>local_tomcat</server>  
  23.                 <ignorePackaging>true</ignorePackaging>  
  24.                 <contextReloadable>true</contextReloadable>  
  25.                 <!-- server、username、password对应maven的setting下的配置 -->  
  26.                 <server>  
  27.                     <id>tomcat</id>  
  28.                     <username>admin</username>  
  29.                     <password>admin</password>  
  30.                 </server>  
  31.                 <!--  <path>/${project.build.finalName}</path> -->  
  32.                                        <!-- war文件路径缺省情况下指向target -->  
  33.                                        <!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile>-->  
  34.             </configuration>  
  35.         </plugin>  
  36.     </plugins>  
  37. </build>  
3、命令打包

在部署之前,必须先启动tomcat7服务,C:\Program Files\apache-tomcat-7.0.39\bin\startup.bat

找到要部署的工程文件根目录下,执行如下maven命令

> mvn clean install     //clean是清理输出文件,install编译打包,在每次打包之前必须执行clean,才能保证发布为最新文件


将maven项目打war包并部署到Tomcat上

将maven项目打war包并部署到Tomcat上

4、在F:\JavaProgram\ELK\hxyc\hxyc\web\target目录下找到打好的war包放入 tomcat的/home/elk/apache-tomcat-7.0.57/webapps目录下

5、启动tomcat,之后即可访问自己发布的web页面

将maven项目打war包并部署到Tomcat上

将maven项目打war包并部署到Tomcat上