maven项目部署到Tomcat

1.安装Tomcat,地址:http://tomcat.apache.org,我安装的版本为9.0.0.M22,安装的目录为C:\apache-tomcat,设置环境变量CATALINA_HOME为C:\apache-tomcat,同时添加%CATALINA_HOME%\bin到Path环境变量中

2.在默认情况下,Tomcat Manager是处于禁用状态的。准确地说,Tomcat Manager需要以用户角色进行登录并授权才能使用相应的功能,不过Tomcat并没有配置任何默认的用户,因此需要我们进行相应的用户配置之后才能使用Tomcat Manager。

Tomcat Manager的用户配置是在Tomcat安装目录/conf/tomcat-users.xml文件中进行管理的。打开conf/tomcat-users.xml,添加manager-gui,manager-script角色

<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">

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

以下是Tomcat Manager 4种角色的大致介绍(下面URL中的*为通配符):

manager-gui
允许访问html接口(即URL路径为/manager/html/*)
manager-script
允许访问纯文本接口(即URL路径为/manager/text/*)
manager-jmx
允许访问JMX代理接口(即URL路径为/manager/jmxproxy/*)
manager-status
允许访问Tomcat只读状态页面(即URL路径为/manager/status/*)

此时启动Tomcat,在浏览器中输入地址http://localhost:8080/manager/html,在弹出的对话框里输入manager-gui对应的用户名和密码 (root/123456),看到以下画面,就说明Tomcat Manger配置成功了

3.在maven项目中的pom文件中添加tomcat插件,如下:

  <plugins>
      <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://localhost:8080/manager/text</url>
                <username>admin</username>
                <password>123456</password>
                <update>true</update>
                <path>/hello</path>
            </configuration>
       </plugin>
  </plugins>

其中username和password为manager-script角色对应的用户名和密码(admin/123456)

 完整的pom文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jack</groupId>
  <artifactId>hello</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>hello Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
         <version>3.0.1</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>hello</finalName>
      <plugins>
         <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://localhost:8080/manager/text</url>
                <username>admin</username>
                <password>123456</password>
                <update>true</update>
            </configuration>
          </plugin>
      </plugins>
  </build>
</project>

 然后执行命令:mvn tomcat7:redeploy,就可以将项目部署到Tomcat安装目录/webapps下,如图:

在浏览器中输入http://localhost:8080/hello,就可以看到输出结果了

如需要部署到远程服务器,则需要在远程服务器的conf/Catalina/localhost目录下创建一个manager.xml文件,写入如下值:

<?xml version="1.0" encoding="UTF-8"?>
<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
             <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>

 

posted @ 2017-07-09 15:25  南极冰川雪  阅读(606)  评论(0编辑  收藏  举报