一、进入maven项目主目录执行mvn clean package 打war包(在pom.xml中配置打jar还是war,jar是自带容器,war则需要在其他容器中才能运行,生成的war会在target下)

二、将生成的war放入服务器上tomcat主目录(在bin中执行./startup.sh会自动生成服务器使用项目)
三.这时的访问需要加上端口和项目名(项目名就是服务器生成的文件夹名称)

如果在访问时,想去掉项目名称可以进行以下配置

一、将生成的项目放入ROOT中
二、配置tomcat
 <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
 /docs/cluster-howto.html  (simple how to)
 /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
 via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
 resources under the key "UserDatabase".  Any edits
 that are performed against this UserDatabase are immediately
 available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
 resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
 unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/usr/local/apache/tomcat/tomcat-kun/webapps/xuetang-1.0-SNAPSHOT" />  //配置改路径后访问时只需要输入端口便可以访问
        <!-- SingleSignOn valve, share authentication between web applications
 Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
 Documentation at: /docs/config/valve.html
 Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
 prefix="localhost_access_log" suffix=".txt"
 pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>

扩展:以上需要使用tomcat启动

可以自己配置启动脚本如下tomcat-kun.sh

# tomcat8081Æô¶¯¶Ë¿Ú
TOMCAT1_HOME=/usr/local/apache/tomcat/tomcat-kun #tomcat的主目录
start_tomcat=$TOMCAT1_HOME/bin/startup.sh #tomcat 启动项目开关路径
stop_tomcat=$TOMCAT1_HOME/bin/shutdown.sh #tomcat 关闭项目开关路径
start{ #启动
    echo -n "Starting tomcat1:" #提示语
    ${start_tomcat}
    echo "tomcat8081 start ok"  #提示语
}
stop{ #关闭
    echo -n "Shutdown tomcat"  #提示语
    ${stop_tomcat}
    echo "tomcat8081 stop ok"  #提示语
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 10
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
esac
exit 0

脚本可放置在任意地方(注意:该脚本需要转换为ULIX格式)

使用这种脚本启动的方法:
   进入到该脚本目录  sh tomcat-kun.sh start
使用这种脚本关闭的方法:
   进入到该脚本目录  sh tomcat-kun.sh stop
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐