您的位置:首页 > 运维架构 > Tomcat

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

2016-01-26 11:14 615 查看
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页面



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: