windows下tomcat8啓動腳本代碼剖析--startup.bat

Windows下,Tomcat可以以服務形式啓動、停止,也可以執行腳本啓動(startup.bat)、停止(shutdown.bat)。執行startup.bat時會調用catalina.bat,catalina.bat腳本又會調用setclasspath.bat進行java class path指定。本文將剖析startup.bat的代碼實現。

1、tomcat版本及安裝目錄

版本:8.0.36
安裝目錄:E:\tomcat8

2、startup.bat腳本代碼

@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------

setlocal

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

3、代碼說明

以每行註釋方式,說明每行代碼作用,並移除原代碼中的所有rem註釋。

rem 關閉回顯
@echo off

rem 開啓局部變量
setlocal

rem 設置CURRENT_DIR爲當前目錄,本例爲e:\tomcat8
set "CURRENT_DIR=%cd%"

rem 如果CATALINA_HOME非空,執行gotHome
if not "%CATALINA_HOME%" == "" goto gotHome

rem CATALINA_HOME爲空,則設置CATALINA_HOM=CURRENT_DIR,本例爲e:\tomcat8
set "CATALINA_HOME=%CURRENT_DIR%"

rem 如果存在catalina.bat文件,執行okHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome

rem 如果不存在catalina.bat文件,則返回上級目錄
cd ..

rem 變量賦值
set "CATALINA_HOME=%cd%"

rem 進入CURRENT_DIR目錄
cd "%CURRENT_DIR%"

:gotHome
rem 如果bat文件存在執行okHome,否則輸出錯誤信息,結束腳本。
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end

:okHome
rem 變量賦值
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem "%EXECUTABLE%"存在則執行okExec,本例爲e:\tomcat8\bin\catalina.bat;不存在則報錯並結束腳本
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end

:okExec
set CMD_LINE_ARGS=

rem 參數拼接,保留startup.bat傳入的參數;
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

rem 調用catalina.bat start 拼接startup.bat 傳入的參數
rem 如startup.bat arg1 arg2,則catalina.bat start arg1 arg2
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
:end

整個startup.bat腳本最核心代碼就是調用catalina腳本,實際場景類似:

call E:\tomcat8\bin\catalina.bat start
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章