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

本文介绍了如何在Eclipse环境下,使用Maven 3.3.3将项目打包成WAR文件,并部署到Tomcat 7.0.63服务器上。首先,需要配置Tomcat的manager权限,然后启动Tomcat服务。接着,通过Maven的clean install命令进行编译打包。最后,将生成的WAR包复制到Tomcat的webapps目录下,重启Tomcat即可访问新部署的网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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下做如下修改

<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
-->
	<role rolename="admin-gui"/>
    <role rolename="admin-script"/>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <role rolename="manager-status"/>
    <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
</tomcat-users>
(2)apache-maven-3.3.3配置:为了让maven可以访问tomcat的权限,所以需要把如上创建的用户添加到settings.xml中

<servers>

    <!-- 配置tomcat-/manager/text 访问权限 -->
    <server>
      <id>tomcat</id>
      <username>admin</username>
      <password>admin</password>
    </server>

  </servers>

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

	<build>
	        <!--  <finalName>hxyc</finalName> -->
		<!-- directory缺省情况下指向target --> 
                <!--<directory>${basedir}/target</directory>-->
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<port>8080</port>
					<url>http://localhost:8080/hxyc</url>
					<server>local_tomcat</server>
					<ignorePackaging>true</ignorePackaging>
					<contextReloadable>true</contextReloadable>
					<!-- server、username、password对应maven的setting下的配置 -->
					<server>
						<id>tomcat</id>
						<username>admin</username>
						<password>admin</password>
					</server>
					<!--  <path>/${project.build.finalName}</path> -->
                                        <!-- war文件路径缺省情况下指向target -->
                                        <!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile>-->
				</configuration>
			</plugin>
		</plugins>
	</build>
3、命令打包

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

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

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


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

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


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值